简体   繁体   English

多次导入python模块

[英]Multiple imports of python modules

Just for my knowledge, how does python, especially wxpython reacts to multiple imports? 仅仅据我所知,python,尤其是wxpython如何对多个导入做出反应? If I import wx in multiple files, how does it handle that when called the main frame? 如果我在多个文件中导入wx,它在调用主框架时如何处理? Does it slows the speed or it firstly checks whether it is already been imported or not? 它会降低速度还是首先检查它是否已经导入?

When Python imports a file, it keps track of it by storing it in sys.modules . 当Python导入文件时,它会通过将其存储在sys.modules来跟踪它。 So whenever Python is importing a file it checks there first and, if it finds it there, returns that instead; 因此,每当Python导入文件时,它首先在那里检查,如果在那里找到它,则返回该文件; if it is not there, it imports it, adds it to sys.modules , and then returns it. 如果它不存在,则导入它,将其添加到sys.modules ,然后返回它。

Only the first import executes the file. 只有第一次导入才会执行该文件。 Subsequent imports copy the reference from sys.modules. 后续导入将复制sys.modules中的引用。

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

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