[英]Importing a py file within itself
This is test.py: 这是test.py:
import sys
a = 50
b = [1,2]
def change():
print "Here 1"
import test
print "Here 2"
test.a = -1
test.b = [0,1]
return
def main():
print "Here 3"
change()
print "Here 4"
print a, b
if 1:
main()
The above python code when ran on system generates the following output: 在系统上运行时上面的python代码生成以下输出:
Here 3
Here 1
Here 3
Here 1
Here 2
Here 4
-1 [0, 1]
Here 2
Here 4
50 [1, 2]
What I am confused why is not there an infinite loop of "Here 1 \\n Here 3" outputs. 令我感到困惑的是为什么没有“Here 1 \\ n Here 3”输出的无限循环。 How can the print a, b outputs can be justified?
如何证明打印a,b输出是否合理?
When you run the file as a script, it is not considered to be the test
module. 将文件作为脚本运行时,不会将其视为
test
模块。 It is considered to be the __main__
module. 它被认为是
__main__
模块。
When execution hits import test
, a second execution of the file starts, where the module is considered to be test
. 当执行命中
import test
,文件的第二次执行开始,其中模块被认为是test
。
When execution hits import test
again, Python recognizes that it's already importing test
and does not reexecute the module. 当执行再次进入
import test
时,Python会识别出它已经导入test
并且不会重新执行该模块。 Instead, it merely loads the half-initialized test
module object into the current namespace and continues on. 相反,它只是将半初始化的
test
模块对象加载到当前命名空间中并继续。 Python's optimistic assumption is that you've written the code so that the contents of test
won't be needed until the import finishes. Python的乐观假设是您编写了代码,以便在导入完成之前不需要
test
内容。
When execution hits the assignments to test.a
and test.b
, that affects the test
module, but not __main__
, despite the fact that they came from the same file. 当执行命中
test.a
和test.b
,它会影响test
模块,但不影响__main__
,尽管它们来自同一个文件。 Thus, the print a, b
from the imported module reflects the new values, while the print a, b
from __main__
reflects the initial values. 因此,来自导入模块的
print a, b
反映新值,而来自__main__
的print a, b
反映初始值。
A file can only be imported once. 文件只能导入一次。 The 'import test' line succeeds the first time it is encountered.
“导入测试”行在第一次遇到时成功。 When it is encountered a second time, the interpreter will check that it has already been loaded.
当第二次遇到它时,解释器将检查它是否已被加载。
When a program is initially run, it does not count as being 'imported'. 最初运行程序时,它不算作“导入”。
The general flow of this script is as follows: 该脚本的一般流程如下:
While user2367112's excellent answer explains why this happens, none of the answers here offer a workaround. 虽然user2367112的优秀答案解释了为什么会发生这种情况,但这里的答案都没有提供解决方法。
There are two easy ways to achieve the desired behavior. 有两种简单的方法可以实现所需的行为。
test
, use import __main__
instead. test
,而是使用import __main__
。 If you assign an alias with import __main__ as test
, you won't even have to change any other code. import __main__ as test
的别名指定import __main__ as test
,则甚至不必更改任何其他代码。 sys.modules['test'] = sys.modules['__main__']
to tell python "Hey, this module already exists". sys.modules['test'] = sys.modules['__main__']
告诉python“嘿,这个模块已经存在”。 After this, import test
will not re-import the module, thus making your code work as expected. import test
将不会重新导入模块,从而使您的代码按预期工作。 The relevant docs on sys.modules
can be found here . sys.modules
上的相关文档可以在这里找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.