python 多次导入时python会优化模块吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:50:38  来源:igfitidea点击:

Does python optimize modules when they are imported multiple times?

pythonpython-import

提问by JimB

If a large module is loaded by some submodule of your code, is there any benefit to referencing the module from that namespace instead of importing it again?

如果代码的某个子模块加载了一个大模块,那么从该命名空间中引用该模块而不是再次导入它有什么好处吗?

For example: I have a module MyLib, which makes extensive use of ReallyBigLib. If I have code that imports MyLib, should I dig the module out like so

例如:我有一个模块 MyLib,它广泛使用了 RealBigLib。如果我有导入 MyLib 的代码,我应该像这样挖出模块吗

import MyLib
ReallyBigLib = MyLib.SomeModule.ReallyBigLib

or just

要不就

import MyLib
import ReallyBigLib

回答by Toni Ru?a

Python modules could be considered as singletons... no matter how many times you import them they get initialized only once, so it's better to do:

Python 模块可以被视为单例......无论你导入它们多少次,它们只会被初始化一次,所以最好这样做:

import MyLib
import ReallyBigLib

Relevant documentation on the import statement:

导入语句的相关文档:

https://docs.python.org/2/reference/simple_stmts.html#the-import-statement

https://docs.python.org/2/reference/simple_stmts.html#the-import-statement

Once the name of the module is known (unless otherwise specified, the term “module” will refer to both packages and modules), searching for the module or package can begin. The first place checked is sys.modules, the cache of all modules that have been imported previously. If the module is found there then it is used in step (2) of import.

一旦知道模块的名称(除非另有说明,术语“模块”将同时指包和模块),就可以开始搜索模块或包。首先检查的是 sys.modules,这是之前导入的所有模块的缓存。如果在那里找到该模块,则在导入的步骤 (2) 中使用它。

The imported modules are cached in sys.modules:

导入的模块缓存在sys.modules 中

This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. Note that removing a module from this dictionary is not the same as calling reload() on the corresponding module object.

这是一个将模块名称映射到已加载模块的字典。这可以被操纵以强制重新加载模块和其他技巧。请注意,从此字典中删除模块与在相应的模块对象上调用 reload() 不同。

回答by Charles Merriam

As others have pointed out, Python maintains an internal list of all modules that have been imported. When you import a module for the first time, the module (a script) is executed in its own namespace until the end, the internal list is updated, and execution of continues after the import statement.

正如其他人指出的那样,Python 维护了一个所有已导入模块的内部列表。当你第一次导入一个模块时,模块(一个脚本)在它自己的命名空间中执行直到结束,更新内部列表,并在导入语句之后继续执行。

Try this code:

试试这个代码:

   # module/file a.py
   print "Hello from a.py!"
   import b

   # module/file b.py
   print "Hello from b.py!"
   import a

There is no loop: there is only a cache lookup.

没有循环:只有缓存查找。

>>> import b
Hello from b.py!
Hello from a.py!
>>> import a
>>>

One of the beauties of Python is how everything devolves to executing a script in a namespace.

Python 的优点之一是一切都可以在命名空间中执行脚本。

回答by Federico A. Ramponi

It makes no substantial difference. If the big module has already been loaded, the second import in your second example does nothing except adding 'ReallyBigLib' to the current namespace.

它没有实质性的区别。如果已经加载了大模块,则第二个示例中的第二个导入除了将“ReallyBigLib”添加到当前命名空间之外什么都不做。

回答by Munhitsu

WARNING: Python does not guarantee that module will not be initialized twice. I've stubled upon such issue. See discussion: http://code.djangoproject.com/ticket/8193

警告:Python 不保证模块不会被初始化两次。我在这样的问题上绊倒了。见讨论:http: //code.djangoproject.com/ticket/8193

回答by babbageclunk

The internal registry of imported modules is the sys.modulesdictionary, which maps module names to module objects. You can look there to see all the modules that are currently imported.

导入模块的内部注册表是sys.modules字典,它将模块名称映射到模块对象。您可以在那里查看当前导入的所有模块。

You can also pull some useful tricks (if you need to) by monkeying with sys.modules- for example adding your own objects as pseudo-modules which can be imported by other modules.

您还可以通过玩弄来获取一些有用的技巧(如果需要)sys.modules- 例如将您自己的对象添加为可由其他模块导入的伪模块。

回答by Marko

It is the same performancewise. There is no JIT compiler in Python yet.

性能上是一样的。Python 中还没有 JIT 编译器。