从字符串变量导入模块

问题:从字符串变量导入模块

我正在编写有关matplotlib(MPL)嵌套库的文档(个人),该文档与感兴趣的子模块包所提供的MPL有所不同。我正在编写Python脚本,希望该脚本可以自动从将来的MPL版本中生成文档。
我选择了感兴趣的子模块/程序包,并希望列出其主要类,然后从中生成列表并进行处理。pydoc

问题是我找不到指示Python从字符串加载子模块的方法。这是我尝试过的示例:

import matplotlib.text as text
x = dir(text)

i = __import__('matplotlib.text')
y = dir(i)

j = __import__('matplotlib')
z = dir(j)

这是通过pprint比较上述列表的三种方式:

我不明白y对象中加载了什么-它是基础对象matplotlib以及其他内容,但是它缺少我想要的信息,而这正是matplotlib.text包中的主要类。它是屏幕截图中最上面的蓝色部分(x列表)

请不要建议Sphinx作为其他方法。

I’m working on a documentation (personal) for nested matplotlib (MPL) library, which differs from MPL own provided, by interested submodule packages. I’m writing Python script which I hope will automate document generation from future MPL releases.
I selected interested submodules/packages and want to list their main classes from which I’ll generate list and process it with pydoc

Problem is that I can’t find a way to instruct Python to load submodule from string. Here is example of what I tried:

import matplotlib.text as text
x = dir(text)

.

i = __import__('matplotlib.text')
y = dir(i)

.

j = __import__('matplotlib')
z = dir(j)

And here is 3 way comparison of above lists through pprint:

I don’t understand what’s loaded in y object – it’s base matplotlib plus something else, but it lack information that I wanted and that is main classes from matplotlib.text package. It’s top blue coloured part on screenshot (x list)

Please don’t suggest Sphinx as different approach.


回答 0

__import__功能可能有点难以理解。

如果你改变

i = __import__('matplotlib.text')

i = __import__('matplotlib.text', fromlist=[''])

然后i将参考matplotlib.text

在Python 2.7和Python 3.1或更高版本中,可以使用importlib

import importlib

i = importlib.import_module("matplotlib.text")

一些注意事项

  • 如果您尝试从子文件夹(例如)中导入内容./feature/email.py,则代码将如下所示importlib.import_module("feature.email")

  • 如果__init__.py您要导入的文件所在的文件夹中没有任何内容,则无法导入任何内容

The __import__ function can be a bit hard to understand.

If you change

i = __import__('matplotlib.text')

to

i = __import__('matplotlib.text', fromlist=[''])

then i will refer to matplotlib.text.

In Python 2.7 and Python 3.1 or later, you can use importlib:

import importlib

i = importlib.import_module("matplotlib.text")

Some notes

  • If you’re trying to import something from a sub-folder e.g. ./feature/email.py, the code will look like importlib.import_module("feature.email")

  • You can’t import anything if there is no __init__.py in the folder with file you are trying to import


回答 1

importlib.import_module是您要寻找的。它返回导入的模块。(仅适用于Python> = 2.7或3.x):

import importlib

mymodule = importlib.import_module('matplotlib.text')

之后,您可以访问模块中的任何内容mymodule.myclass,例如等等。

importlib.import_module is what you are looking for. It returns the imported module. (Only available for Python >= 2.7 or 3.x):

import importlib

mymodule = importlib.import_module('matplotlib.text')

You can thereafter access anything in the module as mymodule.myclass, etc.


回答 2

花了一些时间尝试从列表中导入模块,而这正是使线程到达我那里的大部分方式-但是我不了解___import____的用法-

因此,这是从字符串导入模块并获得与导入相同的行为的方法。并尝试/排除错误情况。:)

  pipmodules = ['pycurl', 'ansible', 'bad_module_no_beer']
  for module in pipmodules:
      try:
          # because we want to import using a variable, do it this way
          module_obj = __import__(module)
          # create a global object containging our module
          globals()[module] = module_obj
      except ImportError:
          sys.stderr.write("ERROR: missing python module: " + module + "\n")
          sys.exit(1)

是的,对于python 2.7>,您还有其他选择-但是对于2.6 <,这可行。

spent some time trying to import modules from a list, and this is the thread that got me most of the way there – but I didnt grasp the use of ___import____ –

so here’s how to import a module from a string, and get the same behavior as just import. And try/except the error case, too. :)

  pipmodules = ['pycurl', 'ansible', 'bad_module_no_beer']
  for module in pipmodules:
      try:
          # because we want to import using a variable, do it this way
          module_obj = __import__(module)
          # create a global object containging our module
          globals()[module] = module_obj
      except ImportError:
          sys.stderr.write("ERROR: missing python module: " + module + "\n")
          sys.exit(1)

and yes, for python 2.7> you have other options – but for 2.6<, this works.


回答 3

我开发了以下3个有用的功能:

def loadModule(moduleName):
    module = None
    try:
        import sys
        del sys.modules[moduleName]
    except BaseException as err:
        pass
    try:
        import importlib
        module = importlib.import_module(moduleName)
    except BaseException as err:
        serr = str(err)
        print("Error to load the module '" + moduleName + "': " + serr)
    return module

def reloadModule(moduleName):
    module = loadModule(moduleName)
    moduleName, modulePath = str(module).replace("' from '", "||").replace("<module '", '').replace("'>", '').split("||")
    if (modulePath.endswith(".pyc")):
        import os
        os.remove(modulePath)
        module = loadModule(moduleName)
    return module

def getInstance(moduleName, param1, param2, param3):
    module = reloadModule(moduleName)
    instance = eval("module." + moduleName + "(param1, param2, param3)")
    return instance

每次我想重新加载新实例时,都只需要像这样调用getInstance()即可:

myInstance = getInstance("MyModule", myParam1, myParam2, myParam3)

最后,我可以调用新实例中的所有函数:

myInstance.aFunction()

这里唯一的特殊性是自定义实例的参数列表(param1,param2,param3)。

I developed these 3 useful functions:

def loadModule(moduleName):
    module = None
    try:
        import sys
        del sys.modules[moduleName]
    except BaseException as err:
        pass
    try:
        import importlib
        module = importlib.import_module(moduleName)
    except BaseException as err:
        serr = str(err)
        print("Error to load the module '" + moduleName + "': " + serr)
    return module

def reloadModule(moduleName):
    module = loadModule(moduleName)
    moduleName, modulePath = str(module).replace("' from '", "||").replace("<module '", '').replace("'>", '').split("||")
    if (modulePath.endswith(".pyc")):
        import os
        os.remove(modulePath)
        module = loadModule(moduleName)
    return module

def getInstance(moduleName, param1, param2, param3):
    module = reloadModule(moduleName)
    instance = eval("module." + moduleName + "(param1, param2, param3)")
    return instance

And everytime I want to reload a new instance I just have to call getInstance() like this:

myInstance = getInstance("MyModule", myParam1, myParam2, myParam3)

Finally I can call all the functions inside the new Instance:

myInstance.aFunction()

The only specificity here is to customize the params list (param1, param2, param3) of your instance.


回答 4

除了使用importlib一个,还可以使用exec方法从字符串变量导入模块。

在这里,我展示了使用combinations方法从itertools包中导入方法的示例exec

MODULES = [
    ['itertools','combinations'],
]

for ITEM in MODULES:
    import_str = "from {0} import {1}".format(ITEM[0],', '.join(str(i) for i in ITEM[1:]))
    exec(import_str)

ar = list(combinations([1, 2, 3, 4], 2))
for elements in ar:
    print(elements)

输出:

(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

Apart from using the importlib one can also use exec method to import a module from a string variable.

Here I am showing an example of importing the combinations method from itertools package using the exec method:

MODULES = [
    ['itertools','combinations'],
]

for ITEM in MODULES:
    import_str = "from {0} import {1}".format(ITEM[0],', '.join(str(i) for i in ITEM[1:]))
    exec(import_str)

ar = list(combinations([1, 2, 3, 4], 2))
for elements in ar:
    print(elements)

Output:

(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)