Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的动态模块加载范围_Python_Dynamic_Module_Scope - Fatal编程技术网

Python中的动态模块加载范围

Python中的动态模块加载范围,python,dynamic,module,scope,Python,Dynamic,Module,Scope,当我运行以下示例时: def a(): exec('import math') b() def b(): print math.cos(90) a() 我得到以下错误: 名称错误:未定义全局名称“数学” 我试图做的是从a()函数中动态加载一些模块 并在函数b()中使用它们 我希望它从b()的角度来看尽可能无缝。这意味着,我不想在a()中加载带有)的模块,并传递对b()函数的引用,事实上,b()的 有什么办法可以做到这一点吗? 谢谢 Python2.x的一种方法是:

当我运行以下示例时:

def a():
    exec('import math')
    b()

def b():
    print math.cos(90)

a()
我得到以下错误: 名称错误:未定义全局名称“数学”

我试图做的是从a()函数中动态加载一些模块 并在函数b()中使用它们

我希望它从b()的角度来看尽可能无缝。这意味着,我不想在a()中加载带有)的模块,并传递对b()函数的引用,事实上,b()的

有什么办法可以做到这一点吗?
谢谢

Python2.x的一种方法是:

def a():
    exec 'import math' in globals()
    b()

def b():
    print math.cos(90)

a()
但我通常会建议使用
\uuuuu import\uuuu()
。我不知道你到底想实现什么,但也许这对你有用:

def a():
    global hurz
    hurz = __import__("math")
    b()

def b():
    print hurz.cos(90)

a()

Python2.x的一种方法是:

def a():
    exec 'import math' in globals()
    b()

def b():
    print math.cos(90)

a()
但我通常会建议使用
\uuuuu import\uuuu()
。我不知道你到底想实现什么,但也许这对你有用:

def a():
    global hurz
    hurz = __import__("math")
    b()

def b():
    print hurz.cos(90)

a()

根据帖子上的评论:如果要加载模块运行时,请在需要的地方加载:

def b():
  m = __import__("math")
  return m.abs(-1)
回答你的问题:

def a():
  if not globals().has_key('math'):
    globals()['math'] = __import__('math')

def b():
  """returns the absolute value of -1, a() must be called before to load necessary modules"""
  return math.abs(-1)

根据帖子上的评论:如果要加载模块运行时,请在需要的地方加载:

def b():
  m = __import__("math")
  return m.abs(-1)
回答你的问题:

def a():
  if not globals().has_key('math'):
    globals()['math'] = __import__('math')

def b():
  """returns the absolute value of -1, a() must be called before to load necessary modules"""
  return math.abs(-1)

为什么不在包含
b()
的文件顶部导入数学呢。。。我不是问“为什么?”。也许
globals()?看起来很难看:)因为我事先不知道我想使用数学模块。“数学”只是一个例子。我想在运行时从a()中加载任何模块function@khachik老兄,你救了我的命@卡奇克:我怎么能投你的票?total stackoverflow noob此处为什么不在包含
b()的文件顶部导入数学
。。。我不是问“为什么?”。也许
globals()?看起来很难看:)因为我事先不知道我想使用数学模块。“数学”只是一个例子。我想在运行时从a()中加载任何模块function@khachik老兄,你救了我的命@卡奇克:我怎么能投你的票?总堆栈溢出noob这里为什么
m=\u导入(“数学”)
而不仅仅是
导入数学为m
?这就是我想要的谢谢-每当我得到分数我都会投票this@aaronasterling因为我是在
a
中放入
globals()
后才想到这个问题的,没什么特别的。@Alexandros接受它怎么样?或者你认为正确的任何其他答案。为什么<代码> M=α导入(“数学”)< /代码>而不只是<代码>导入数学为m < /代码>?这就是我要感谢的——每当我得到分数时,我将投票。this@aaronasterling因为我是在
a
中放入
globals()
后得出这个结论的,没什么特别的。@Alexandros接受它怎么样?或者其他任何你认为正确的答案。这也是一个很好的回答。这也是一个很好的答案。