Ok, this is not very clear at all. Allow me to rephrase.
Quick note: "Solved" while writing this question. Will accept answer with inputs regarding best practices.
Original quick note: This is probably a duplicate. I apparently couldn't phrase the question well enough to fit this situation. My apologies if it is.
First of all, to get you situated, here is my project's hierarchy as well as some relevant code:
Project/
├── main.py
└── lib
├── __init__.py
├── A.py
├── B.py
└── C.py
main.py
:
## Imports
from lib.A import ClassA as clA
from lib.B import ClassB as clB
from lib.C import ClassC as clC
#
# ... Some other code ...
#
a = clA()
b = clB()
a.add_ClB_Inst_Ref(b)
A.py
:
## Imports
if __name__ == "__main__":
from B import ClassB as clB
from C import ClassC as clC
class ClassA:
dict_Of_Weighted_ClB_Refs = {}
#
# ... Some other code (attributes, init, ...) ...
#
def add_ClB_Inst_Ref(self, ClB_Instance):
if isinstance(ClB_Instance, clB):
key = clB.ID
if key in self.dict_Of_Weighted_ClB_Refs:
self.dict_Of_Weighted_ClB_Refs[key] +=1
else:
self.dict_Of_Weighted_ClB_Refs[key] = 1
Issue:
The program crashes in the add_ClB_Inst_Ref
method when checking that the method's parameter is an instance of the ClassB object with the following error:
NameError: name 'clB' is not defined
"Well", you might say, "of course it crashes, the A.py
files has never heard of that fancy clB
!".
And, yes this may be the problem, but when I try to import it inside this file, replacing the imports section with this:
## Imports
from B import ClassB as clB
from C import ClassC as clC
I get the following error: ImportError: No module named 'B'
Question:
How can I fix this? Your input on best practices would be most welcomed.
Suspicions:
All this leads me to these hypothesis:
- Modules imported in a file at the root of the project aren't globally available for the duration of the program's execution.
import
searches a match from where the initial script has been executed. (Note: This lead me to double check that I had tried it this wayfrom lib.B import ClassB as clB
just like in the main file; turns out I hadn't and this works... I get the logic, but it seems rather counter intuitive. See answer below for details.)- My project's architecture is flawed.