Here is my code below.
main.py:
import moduleA
print('It works!')
moduleA.py:
import moduleB
def functionA():
return 0
moduleB.py:
import moduleA
globalVariableFromModuleB = moduleA.functionA()
If I run it, I get the error message:
$ python main.py
Traceback (most recent call last):
File "main.py", line 3, in <module>
import moduleA
File "/home/asa/bugs/moduleA.py", line 3, in <module>
import moduleB
File "/home/asa/bugs/moduleB.py", line 8, in <module>
globalVariableFromModuleB_1 = functionB()
File "/home/asa/bugs/moduleB.py", line 6, in functionB
return moduleA.functionA()
Q1: In my case moduleB explicitly imports moduleA, thus I expect it to work, but it does not. Is this because Python cashes imports and does not do it twice? But why it cannot then take the already cashed moduleA.functionA() from the memory instead of failing? I suppose the current behavior is a bug therefore.
Q2: If I remove the line "globalVariableFromModuleB = moduleA.functionA()" and leave only the cyclic import "import moduleA" then I have no failure. So, cyclic dependencies are not forbidden in Python. For what are they allowed if they don't work correclty as it shows my example?
Q3: If I change "import moduleA" to "from moduleA import functionA" the main program does not work either failing with another message "ImportError: cannot import name functionA".
Also I would like to post here a workaround for the people who don't like to redesign the application, like in my case.
Workaround (found it just experimentally). To add "import moduleB" before the "import moduleA" in the main.py, that is:
# main.py
import moduleB
import moduleA
print('It works!')
But I had to leave a long comment right at this import in the code, because main.py does not use directly any API from moduleB, so it is looking ugly.
Could one suggest some better wa how to resolve such case and answer 3 questions above, please?