I have a package my_package/ which contains two modules module1.py and module2.py, where module2.py contains:
import sys
print(sys.path) # for troubleshooting
from my_package import module1
Running:
python -c "from my_package import module2"
from the parent directory works fine. However I would also like to be able to run module2.py for unit testing. Running:
ipython my_package/module2.py
also works fine. However trying to run with python:
python my_package/module2.py
fails (ModuleNotFoundError: No module named 'my_package'). In both cases, /home/user/my_project/my_package appears in sys.path. My versions of python and ipython are the same (3.7.4). Adding __init__.py in my_package/ did not help.
Why does this happen, and how do I solve this issue?