What will happen if two modules import each other?
To generalize the problem, what about the cyclic imports in Python?
I got an example here that struck me!
foo.py
import bar
class gX(object):
g = 10
bar.py
from foo import gX
o = gX()
main.py
import foo
import bar
print "all done"
At the command line: $ python main.py
Traceback (most recent call last):
File "m.py", line 1, in <module>
import foo
File "/home/xolve/foo.py", line 1, in <module>
import bar
File "/home/xolve/bar.py", line 1, in <module>
from foo import gX
ImportError: cannot import name gX
There was a really good discussion on this over at comp.lang.python last year. It answers your question pretty thoroughly.
Imports are pretty straightforward really. Just remember the following:
'import' and 'from xxx import yyy' are executable statements. They execute when the running program reaches that line.
If a module is not in sys.modules, then an import creates the new module entry in sys.modules and then executes the code in the module. It does not return control to the calling module until the execution has completed.
If a module does exist in sys.modules then an import simply returns that module whether or not it has completed executing. That is the reason why cyclic imports may return modules which appear to be partly empty.
Finally, the executing script runs in a module named __main__, importing the script under its own name will create a new module unrelated to __main__.
Take that lot together and you shouldn't get any surprises when importing modules.
I solved the problem the following way, and it works well without any error.
Consider two files a.py
and b.py
.
I added this to a.py
and it worked.
if __name__ == "__main__":
main ()
import b
y = 2
def main():
print ("a out")
print (b.x)
if __name__ == "__main__":
main ()
import a
print ("b out")
x = 3 + a.y
The output I get is
>>> b out
>>> a out
>>> 5
If you do import foo
(inside bar.py
) and import bar
(inside foo.py
), it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.
The problem is when instead you do from foo import abc
(inside bar.py
) and from bar import xyz
(inside foo.py
). Because now each module requires the other module to already be imported (so that the name we are importing exists) before it can be imported.
Module a.py :
import b
print("This is from module a")
Module b.py
import a
print("This is from module b")
Running "Module a" will output:
>>>
'This is from module a'
'This is from module b'
'This is from module a'
>>>
It output this 3 lines while it was supposed to output infinitival because of circular importing. What happens line by line while running"Module a" is listed here:
import b
. so it will visit module bimport a
. so it will visit module aimport b
but note that this line won't be executed again anymore, because every file in python execute an import line just for once, it does not matter where or when it is executed. so it will pass to the next line and print "This is from module a"
."This is from module b"
"This is from module a"
and program will be finished.Suppose you are running a test python file named request.py
In request.py, you write
import request
so this also most likely a circular import.
Solution
Just change your test file to another name such as aaa.py
, other than request.py
.