My first question on stack! :D
I have a problem when I try to have my cake and eat it too, it seems. I have stripped out all the code as I do not believe that it is part of the problem.
I have the following dir structure:
master/
- main.py
- modules/
- parent_class.py
- child_class.py
- __init__.py
In parent_class.py:
class Parent:
pass
In child_class.py:
from modules.parent_class import Parent
class Child(Parent)
pass
if __name__ == "__main__":
child = Child()
child.do_stuff()
In main.py:
from modules.child_class import Child
child = Child()
child.do_stuff()
The problem I am having I believe it has to do with me not understanding sys.path properly.
When I run main.py there are no errors. However, when I try to run child_class.py for testing purposes I get the following error...
Traceback (most recent call last):
File "child_class.py", line 1, in <module>
from modules.parent_class import Parent
ModuleNotFoundError: No module named 'modules'
The error goes away when I change child_class.py to this:
from parent_class import Parent
class Child(Parent)
pass
if __name__ == "__main__":
child = Child()
child.do_stuff()
But now when I run main.py I get this error:
Traceback (most recent call last):
File "c.../main.py", line 1, in <module>
from modules.child_class import Child
File "...\child_class.py", line 1, in <module>
from parent_class import Parent
ModuleNotFoundError: No module named 'parent_class'
How do you do a unit test if you have to change the import line every time? Thank you in advance for a good explaination. (I have read a lot of docs on importing, and packages and modules, watched like 10 different vids on this topic but still not sure why or how to make this just work.) (I am just saying I have tried to find the answer, but I am now exhausted and need a solution before I really go mad!) thank you, thank you, thank you