如何从函数中进行全局导入?

时间:2012-08-16 15:26:02

标签: python import module python-module

我担心这是解决问题的一种混乱方式,但......

假设我想根据某些条件在Python中进行一些导入。

出于这个原因,我想写一个函数:

def conditional_import_modules(test):
    if test == 'foo':
        import onemodule, anothermodule
    elif test == 'bar':
        import thirdmodule, and_another_module
    else:
        import all_the_other_modules

现在我如何才能在全球范围内使用导入的模块?

例如:

conditional_import_modules(test='bar')
thirdmodule.myfunction()

9 个答案:

答案 0 :(得分:61)

导入的模块只是变量 - 绑定到某些值的名称。因此,您只需要导入它们并使用global关键字将其全局化。

示例:

>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> def f():
...     global math
...     import math
...
>>> f()
>>> math
<module 'math' from '/usr/local/lib/python2.6/lib-dynload/math.so'>

答案 1 :(得分:9)

您可以在以下函数中进行全局导入:

def my_imports(module_name):
    globals()[module_name] = __import__(module_name)

答案 2 :(得分:2)

您可以让此函数返回要导入的模块的名称,然后使用

mod == __import__(module_name)

答案 3 :(得分:2)

您可以使用内置函数__import__有条件地导入具有全局范围的模块。

要导入顶级模块(请注意:import foo):

def cond_import():
  global foo
  foo = __import__('foo', globals(), locals()) 

从层次结构导入(想想:import foo.bar):

def cond_import():
  global foo
  foo = __import__('foo.bar', globals(), locals()) 

从层次结构和别名导入(想想:import foo.bar as bar):

def cond_import():
  global bar
  foo = __import__('foo.bar', globals(), locals()) 
  bar = foo.bar

答案 4 :(得分:1)

我喜欢@badzil方法。

def global_imports(modulename,shortname = None, asfunction = False):
    if shortname is None: 
        shortname = modulename
    if asfunction is False:
        globals()[shortname] = __import__(modulename)
    else:        
        globals()[shortname] = eval(modulename + "." + shortname)

传统上在类模块中的东西:

import numpy as np

import rpy2
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.packages import importr

可以转换为全球范围:

global_imports("numpy","np")

global_imports("rpy2")
global_imports("rpy2.robjects","robjects")
global_imports("rpy2.robjects.packages","rpackages")
global_imports("rpy2.robjects.packages","importr",True)

可能有一些错误,我将验证并更新。最后一个例子也可以有一个别名,这个别名是另一个&#34;短名称&#34;或像#34; importr | aliasimportr&#34;

这样的黑客

答案 5 :(得分:1)

我刚遇到类似的问题,这是我的解决方案:

class GlobalImport:

    def __enter__(self):
        return self

    def __call__(self):
        import inspect
        self.collector = inspect.getargvalues(inspect.getouterframes(inspect.currentframe())[1].frame).locals

    def __exit__(self, *args):
        globals().update(self.collector)

然后,在代码中的任意位置:

with GlobalImport() as gi:
    import os, signal, atexit, threading, _thread
    # whatever you want it won't remain local
    # if only 
    gi()
    # is called before the end of this block

# there you go: use os, signal, ... from whatever place of the module

答案 6 :(得分:1)

步骤1:同一目录/文件夹中的config.py,config_v2.py,rnd.py

第2步:config.py

HIGH_ATTENDANCE_COUNT_MIN = 0

第3步:config_v2.py

HIGH_ATTENDANCE_COUNT_MIN = 5

第4步:rnd.py

def versioning_test(v):
    global config

    if v == 'v1':
        config = __import__('config', globals(), locals()) 
    
    if v == 'v2':
        config = __import__('config_v2', globals(), locals())     

def version_test_in_another_function():
    print('version_test_in_another_function: HIGH_ATTENDANCE_COUNT_MIN: ', config.HIGH_ATTENDANCE_COUNT_MIN)
 

versioning_test("v2")
version_test_in_another_function()

第5步:$ python3 rnd.py

<<output>>: version_test_in_another_function: HIGH_ATTENDANCE_COUNT_MIN:  5

答案 7 :(得分:0)

我喜欢@rafał抓斗方法。由于它甚至支持全部导入。 即 从os import *

(尽管这不是XD的好习惯)

不允许发表评论,但这是python 2.7版本。

还消除了最后调用该函数的需要。

class GlobalImport:
    def __enter__(self):
        return self
    def __exit__(self, *args):
        import inspect
        collector = inspect.getargvalues(inspect.getouterframes(inspect.currentframe())[1][0]).locals
        globals().update(collector)

def test():
    with GlobalImport() as gi:
        ## will fire a warning as its bad practice for python. 
        from os import *

test()
print path.exists(__file__)

答案 8 :(得分:0)

我喜欢@maxschlepzig的答案。

该方法存在一个错误,即如果您直接导入一个函数将无法正常工作。 例如,

global_imports("tqdm", "tqdm, True)

不起作用,因为未导入模块。还有这个

global_imports("tqdm")
global_imports("tqdm", "tqdm, True)

有效。

我稍微改变了@maxschlepzig的答案。使用fromlist,以便您可以以统一的方式使用“ From”语句加载函数或模块。

def global_imports(object_name: str,
                   short_name: str = None,
                   context_module_name: str = None):
    """import from local function as global import

    Use this statement to import inside a function,
    but effective as import at the top of the module.

    Args:
        object_name: the object name want to import,
                     could be module or function
        short_name: the short name for the import
        context_module_name: the context module name in the import

    example usage:
    import os -> global_imports("os")
    import numpy as np -> global_imports("numpy", "np")
    from collections import Counter ->
        global_imports("Counter", None, "collections")
    from google.cloud import storage ->
        global_imports("storage", None, "google.cloud")

    """
    if not short_name:
        short_name = object_name
    if not context_module_name:
        globals()[short_name] = __import__(object_name)
    else:
        context_module = __import__(context_module_name,
                                    fromlist=[object_name])
        globals()[short_name] = getattr(context_module, object_name)