Importing a py file within itself

旧巷老猫 提交于 2020-01-10 05:29:06

问题


This is 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:

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. How can the print a, b outputs can be justified?


回答1:


When you run the file as a script, it is not considered to be the test module. It is considered to be the __main__ module.

When execution hits import test, a second execution of the file starts, where the module is considered to be test.

When execution hits import test again, Python recognizes that it's already importing test and does not reexecute the module. Instead, it merely loads the half-initialized test module object into the current namespace and continues on. 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.

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. Thus, the print a, b from the imported module reflects the new values, while the print a, b from __main__ reflects the initial values.




回答2:


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'.




回答3:


The general flow of this script is as follows:

  1. Main is run, so it prints 'Here 3'
  2. change is called, so it prints 'Here 1'
  3. When importing test, python runs the main function of test
  4. When calling change the second time, python is smart enough to know that test is already imported, so it effectively skips that line.
  5. The imported main finishes running
  6. The original script finishes running.



回答4:


While user2367112's excellent answer explains why this happens, none of the answers here offer a workaround.

There are two easy ways to achieve the desired behavior.

  1. Rather than importing test, use import __main__ instead. If you assign an alias with import __main__ as test, you won't even have to change any other code.
  2. You can set sys.modules['test'] = sys.modules['__main__'] to tell python "Hey, this module already exists". After this, import test will not re-import the module, thus making your code work as expected. The relevant docs on sys.modules can be found here.


来源:https://stackoverflow.com/questions/21326245/importing-a-py-file-within-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!