I have a python project with a folders and file structure like:
root directory
classes
my_common_class.py
my_class_1.py
my_class_2.py
my_virtual_env
bla bla bla
my_wonderful_script.py
Both my_class_1.py
& my_class_2.py
are used on my_wonderful_script.py
and both of them inherit from my_common_class.py
.
On each class I have something like:
try:
from classes.my_common_class import myCommonClass
except ImportError:
from my_common_class import myCommonClass
This approach feels a bit ugly. This is because on each of those classes I test each method executing each class file:
if __name__ == '__main__':
#My quick methods testing goes here
....
....
When I activate the virtual enviroment on my local machine, and all tests and my_wonderful_script.py
run smoothly. When I do the same on my server, I get an exception:
File "<path>/my_wonderful_script.py", line 5, in <module>
from classes.my_class_1 import myClass1
File "<path>//classes/my_class_1.py", line 10, in <module>
from my_common_class import myCommonClass
ModuleNotFoundError: No module named 'my_common_class'
I assume I am missing something in the way I am handling the import on each machine.
Is there a "prettier" way to do this?