Crash, Burn, Rinse and Repeat Your Slow Climb out of Circular Import Hell
In our previous episode I had fallen into Circular Import Hell and couldn't find my way out.
Many trial attempts and fall backs followed.
Now however, the slow climb out of the hell hole is taking shape. I am cleaning out cross-references to modules that have not yet been fully imported. (Circular Hell occurs when the imports are partial rather than complete.)
The first aha moment was when I realized I could have each of the successively imported modules (my user-created modules) send me a message, starting with the __init__.py module inside my Package folder doing a print(f"Hi. You have successfully accessed the Package and had it execute your Dunder init code!")
Then, as a fist line in my first to-be imported modules: print(f" (1) Initialization of your vars_01 module (holding your shared variables) has begun").
Then, as a last line in my first to-be imported plural and inter-dependent modules: print(f" (1x) Initialization of your vars_01 module has finished").
Thirdly, as fist line in my second to-be imported modules: print(f" (2) Initialization of your messages_01 module (holding your shared message lines that depend on vars_01 variables) has begun").
These success lines began to appear in my console one after the next.
I was making that slow climb out of the hell hole.
New errors popped up though. Your references in the still-initializing messages module to the vars_01 variables are not recognized (not defined).
Oh no!
UNRECOGNIZABLE
After some fiddling (searching on Google for advice) I come across the idea that after Main imports module_a and begins to import module_b, that module_b should import module_a. And later, module_c should import a first and then b.
More typo's uncovered in the further runs and then, suddenly this: No more circular import problems!
As seen, all the embedded debug print-0uts in modules a, b and c report in on their successful loadings and then we are back in main with a functions-driven printout of the welcome mat.
Text coloration is not yet working. That's a next thing to debug. But we're finally out of hell. Yay,
More to Explore
What is the Python Interpreter? (How does Python Work?)
Getting to know your Python interpreter" - James Bennett
Modules, Packages, Libraries - What's The Difference? --Neural.Nine
Getting to know your Python interpreter" - James Bennett
Modules, Packages, Libraries - What's The Difference? --Neural.Nine
Comments
Post a Comment