简体   繁体   English

import vs __import __()vs importlib.import_module()?

[英]import vs __import__( ) vs importlib.import_module( )?

I noticed Flask was using Werkzeug to __import__ a module, and I was a little confused. 我注意到Flask正在使用Werkzeug __import__一个模块,我有些困惑。 I went and checked out the docs on it and saw that it seems to give you more control somehow in terms of where it looks for the module, but I'm not sure exactly how and I have zero idea how it's different from importlib.import_module . 我去检查了它的文档,发现它似乎在某种程度上为您提供了对模块寻找位置的控制权,但是我不确定确切如何并且我不知道它与importlib.import_module不同。

The odd thing in the Werkzeug example is that it just says __import__(import_name) , so I don't see how that's any different from just using the import statement, since it's ignoring the optional extra parameters. 在Werkzeug示例中,奇怪的是它只说__import__(import_name) ,所以我看不到它与仅使用import语句有什么不同,因为它忽略了可选的额外参数。

Can anyone explain? 谁能解释? I looked at other people having asked similar questions on SO previously but they weren't very clearly phrased questions and the answers didn't address this at all. 我看过以前在SO上问过类似问题的其他人,但他们的措词不是很明确,答案根本没有解决这个问题。

__import__ is a low-level hook function that's used to import modules; __import__是用于导入模块的低级挂钩函数; it can be used to import a module dynamically by giving the module name to import as a variable, something the import statement won't let you do. 通过提供要导入的模块名称作为变量,可以动态地导入模块,而import语句不允许您这样做。

importlib.import_module() is a wrapper around that hook * to produce a nice API for the functionality; importlib.import_module()是该钩子的包装*为该功能生成一个不错的API; it is a very recent addition to Python 2, and has been more fleshed out in Python 3. Codebases that use __import__ generally do so because they want to remain compatible with older Python 2 releases, eg anything before Python 2.7. 它是Python 2的最新添加,并且在Python 3中更加充实。使用__import__代码__import__通常这样做是因为它们希望与旧版本的Python 2兼容,例如Python 2.7之前的任何版本。

One side-effect of using __import__ can be that it returns the imported module and doesn't add anything to the namespace; 使用__import__一个副作用是它返回导入的模块,并且不向名称空间添加任何内容。 you can import with it without having then to delete the new name if you didn't want that new name; 如果您不想使用新名称,可以直接导入,而不必删除新名称; using import somename will add somename to your namespace, but __import__('somename') instead returns the imported module, which you can then ignore. 使用import somename将为您的命名空间添加somename ,但是__import__('somename')会返回导入的模块,您可以忽略该模块。 Werkzeug uses the hook for that reason in one location. 因此,Werkzeug在一个位置使用挂钩。

All other uses are to do with dynamic imports. 所有其他用途都与动态导入有关。 Werkzeug supports Python 2.6 still so cannot use importlib . Werkzeug仍然支持Python 2.6,因此不能使用importlib


* importlib is a Pure-Python implementation, and import_module() will use that implementation, whist __import__ will use a C-optimised version. * importlib是纯Python实现, import_module()将使用该实现,而__import__将使用C优化版本。 Both versions call back to importlib._bootstrap._find_and_load() so the difference is mostly academic. 这两个版本都调用importlib._bootstrap._find_and_load()因此差异主要是学术上的。

__import__(import_name) , so I don't see how that's any different from just using the import statement __import__(import_name) ,所以我看不到与仅使用import语句有什么不同

Both __import__() and importlib.import_module() allow you to import a module when you have the module name as a string. 当模块名称为字符串时, __import__()和importlib.import_module()都允许您导入模块。 You cannot write: 您不能写:

x = 're'
import x

or you'll get: 否则您会得到:

 File "1.py", line 3, in <module>
 import x ImportError: No module named x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM