I tried to read how import works and use what is suggested, but I still have questions:
If I cd to project folder and run python ./test/test1.py I get ModuleNotFoundError: No module named 'lib'.
- If I add
print(os.getcwd())it does print the current directory which isproject. So, why it cannot findlibfolder? - If I
cdtoproject/test, addimport sys sys.path.append('..')to the beginning oftest1.pyand then runpython test1.pyit works. But, why I cannot run it from one level above? - I also tried to add
__init__.pytolibandtestfolders, but I get same error. - If I try changing to relative path
from .lib.lib1 ...and run thetest.pyfromprojectfolder I getImportError: attempted relative import with no known parent packageerror
What am I doing wrong?
test1.py
from lib.lib1 import MyClass
Project structure
project
lib
lib1.py
lib2.py
test
test1.py # from lib.lib1 import MyClass
main.py