使用“__import__”从字符串变量导入模块给出与正常导入语句不同的结果
我正在为嵌套 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 对上述列表进行 3 路比较:
我不明白 y
对象中加载了什么 - 它是基础 matplotlib
加上其他东西,但它缺少我想要的信息,即来自的主类matplotlib.text
包。它是屏幕截图(x
列表)上顶部的蓝色部分。
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
.
The problem is that I can't find a way to instruct Python to load a submodule from a string. Here is an 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 a 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 lacks information that I wanted and that is main classes from matplotlib.text
package. It's the top blue coloured part on screenshot (x
list).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
__import__
函数可能有点难以理解。如果更改
为
,则
i
将引用matplotlib.text
。在 Python 3.1 或更高版本中,您可以使用
importlib
:一些注释
如果您尝试从子文件夹导入某些内容,例如
./feature/email.py
,代码将类似于importlib.import_module("feature.email")
在 Python 3.3 之前,如果您尝试导入的文件所在的文件夹中没有
__init__.py
,则无法导入任何内容(请参阅在决定是否要保留文件以实现向后兼容性(例如与pytest
)之前,请注意。The
__import__
function can be a bit hard to understand.If you change
to
then
i
will refer tomatplotlib.text
.In Python 3.1 or later, you can use
importlib
:Some notes
If you're trying to import something from a sub-folder e.g.
./feature/email.py
, the code will look likeimportlib.import_module("feature.email")
Before Python 3.3 you could not import anything if there was no
__init__.py
in the folder with file you were trying to import (see caveats before deciding if you want to keep the file for backward compatibility e.g. withpytest
).importlib.import_module
就是您要查找的内容为了。它返回导入的模块。此后,您可以访问模块中的任何内容,如
text.myclass
、text.myfunction
等。importlib.import_module
is what you are looking for. It returns the imported module.You can thereafter access anything in the module as
text.myclass
,text.myfunction
, etc.花了一些时间尝试从列表中导入模块,这是让我大部分时间都到了那里的线程 - 但我没有掌握 ___import____ 的用法 -
所以这里是如何从字符串导入模块,并获得相同的行为就像刚刚导入一样。也尝试/排除错误情况。 :)
是的,对于 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. :)
and yes, for python 2.7> you have other options - but for 2.6<, this works.
除了使用
importlib
之外,还可以使用exec
方法从字符串变量导入模块。这里我展示了一个使用
exec
方法从itertools
包导入combinations
方法的示例:输出:
Apart from using the
importlib
one can also useexec
method to import a module from a string variable.Here I am showing an example of importing the
combinations
method fromitertools
package using theexec
method:Output:
您还可以使用
exec
内置 - in 函数将任何字符串作为 Python 代码执行。对我来说,这是解决我的问题的最易读的方法。
You can also use
exec
built-in function that execute any string as a Python code.For me this was the most readable way to solve my problem.
模块自动安装&从列表导入
下面的脚本适用于子模块和伪子模块。
测试:
Module auto-install & import from list
Below script works fine with both submodules and pseudo submodules.
Tests:
我开发了这 3 个有用的函数:
每次我想重新加载一个新实例时,我只需要像这样调用 getInstance() :
最后我可以调用新实例中的所有函数:
这里唯一的特殊性是自定义参数列表(您的实例的 param1、param2、param3)。
I developed these 3 useful functions:
And everytime I want to reload a new instance I just have to call getInstance() like this:
Finally I can call all the functions inside the new Instance:
The only specificity here is to customize the params list (param1, param2, param3) of your instance.