Related questions that do not apply:
- Set PYTHONPATH for local Jupyter Notebook in VS Code - does not apply to running Jupyter server.
- How to set pythonpath at startup when running jupyter notebook server - no answer
- Python: Module Not Found in Jupyter Notebook - solution involves absolute path
- Python module not found <project> only on VPS. works fine on local machine - solution involves absolute path and modifying every notebook
I'm helping to organize some python code with a team at work. For "reasons" the project has multiple subdirectories and so needs PYTHONPATH
or equivalent to add those subdirectories to work. For example, with this project setup…
project/
.venv/
foo/
bar.py
jim/
jam.py
…we want Jupyter notebooks to work for the code:
import jim
import jam
We want these notebooks to work both (a) within VS Code, and (b) when starting a Jupyter notebook server and connecting to it from Google Colab.
Desires:
- Little-to-no setup work needed per user. (Ideally, they just clone the project and run a launch command or manual script from within the .venv to get Jupyter server running.)
- No absolute paths hard-coded anywhere. (Multiple copies of the project sometimes exist on disk, are added and removed; a user needs to be able to launch Jupyter notebook servers for various locations.)
- Specify the
foo
andbar
paths in as few places as possible; ideally one. (There are many, many places where extra paths can be specified within the ecosystem of Python, Jupyter, VS Code, pytest, pylint, etc.) - No need to pollute every single notebook file by adding
sys.path
modifications at the top of each. - Live development is possible. The developers are not installing this project and then using the notebooks, they are iteratively using the notebooks and modifying core library code that affects the next notebook evaluation.
Within VS Code I've gotten the notebooks working by:
Adding
.env
file at the project root with the contents:PYTHONPATH=foo:bar
- The Python extension has a default setting:
"python.envFile" : "${workspaceFolder}/.env"
- The Python extension has a default setting:
Ensuring all notebooks are run (in VS Code) from the root directory by changing the setting
jupyter.notebookFileRoot
from the default${fileDirname}
to${workspaceFolder}
.
I've also had to add the locations in the pyproject.toml
file for testing:
[tool.pytest.ini_options]
pythonpath = [
"foo",
"bar",
]
Now…how can I make it so that all the developers can launch a Jupyter notebook server for the project, inside the project's venv, that gets PYTHONPATH
set correctly? It can be a single launch_jupyter.sh
script, but I'd prefer not to have to maintain the same list of foo:bar
in that script file. (Because it's not DRY, and the actual directory names are longer and more than just a couple.)