My project structure looks like this -
<project_name>
--Makefile
--src
-- __init__.py
-- utils
-- __init__.py
-- general_imports.py
-- variable named X
-- modelling
-- train.py
My train.py module looks like this -
import sys
sys.path.insert(0,'../../src/')
from utils.general_imports import *
print("Value of X is ",X)
Now, this works fine if I run the code from the directory train.py is present in, but I don't want that. I want to make a recipe so it can run from the root directory level, something like this -
make train
Sample Makefile -
train:
python src/modelling/train.py
Please note Makefile is in project root directory.
When I am trying to run my train.py from root level like this - python src/modelling/train.py
, it gives me error - ModuleNotFoundError: No module named 'src/modelling/train'.
To overcome this, I tried to edit the path like this -- sys.path.insert(0,'../../../<project_name>')
and also adding empty __init__.py
on root level too, but this doesnt seem to work either.
Can anyone please help? Also, please suggest any better way to achieve this.
P.S: I dont want to go by way to add/modify PYTHONPATH if it could be possible.
Delete the sys.path hack and the __init__.py
from the <project_name> then run your script from <project_name> as:
python -m src.modelling.train
This is the correct way of running a script (from the parent dir of the root package which is src in your case). Imports are meant to work in this way but not when running the script from its package which you should avoid as you do