What will happen if two modules import each other?
To generalize the problem, what about the cyclic imports in Python?
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.