This must have been asked before, but I cannot for the life of me find it.
I have a project structure something like this
ROOT
├── README.md
├── modules
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py
├── tests
│ ├── test_mod1.py
│ └── test_mod2.py
├── notebooks
│ ├── nb1.ipynb
│ ├── nb2.ipynb
│ └── sub_dir
│ ├── sub_nb.ipynb
│ ├── generate_py
│ └── py_files
│ └── sub_nb.py
├── definitions
└── main.py
So from main.py
I am able to import anything from definitions
, or any module from ROOT/modules
.
What I want is to be able to import from these from anywhere within the notebooks
directory tree. I know I could do this using:
import sys
sys.path.append("..")
But the notebooks
directory tree has many layers, and I don't want my code to start looking like this:
import sys
sys.path.append("../../../../")
What's more, the file generate_py
is a bash script, that converts the jupyter notebooks (.ipynb
) to .py
files and stashes the .py
files into the ./py_files
subdir.
With the above method i end up having to manually edit every file to put an extra ../
into the sys.append()
. This is annoying.
If I run files from within pycharm all works well (I'm guessing it updates your PYTHONPATH to include the project root when you create main.py
?)
To further complicate, this project is run on several machines, shared via git. So absolute references are out.
But running from terminal, or from within jupyter it cant gind modules, or definitions without going through the sys.append()
process. And I feel there must be a better way.
So what is the best practice here?