I have a python project (which I run within a virtualenv) and that has the following structure:
Project
├───.git
├───venv
└───src
├───__init__.py
├───mymodules
│ ├───__init__.py
│ ├───module1.py
│ └───module2.py
└───scripts
├───__init__.py
└───script.py
script.py
import src.mymodules.module1
...
I run the project with venv activated and from the Project directory using the following command:
(venv)$ python src/scripts/script.py
The script runs but gives out the following error before exiting:
Traceback (most recent call last):
File "src/scripts/script.py", line 1, in <module>
import src.mymodules.module1
ImportError: No module named src.mymodules.module1
I have tried running the python shell and trying to import the module from there and it gave no errors. I have _ _init__.py in every directory within src. Is python considering the working directory to be src/scripts? Why is that happening and how can I make src the working directory if that's the case?
sys.path.append('/src')
andsys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
still to no avail. – Kempf