The mechanism of the Python import package is that the import incoming and default system module is placed in the Sys.module dictionary.
Multiple py file import again, will go to sys.module inside check whether already import, if already import, no longer repeat import, otherwise import come in
===
The namespaces for import Glovar and from Comon import Glovar are the same, and key is all Glovar
From Common.glovar import x is different, so from import in test.py, which is equivalent to writing a line of code x = 1 in the test.py file,
At this point, X is the variable in the test's own namespace. So x is only effective in test.py, boring How to modify X, can not affect the X in Glovar
#换种说法from glovar import x#等同于improt glovarx= glovar.x#你调用x的时候,就是自己的命名空间中的x#当你执行x=2时,完全影响不到glovar.x
Here's an explanation.
So, if you need to share a variable, do not use the From file import x form, but instead use the import file, then you can use the file.x, and then file.x= ' ABC ' can be modified. This way, the global variables can be shared. That is, keep a separate namespace so that Python does not import again, allowing for sharing.
Coreki
Links: http://www.jianshu.com/p/4bb742d7d672
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Python multi-module file sharing variables