This repository demonstrates how to set a notebook root for VS Code through a repository.
VS Code Jupyter notebooks will by default treat their local directory as the root.
This creates challenges when attempting to load local data or code within the notebook because the kernal and Python won't have visibility of those files without customization.
A common workaround is to use sys.path
manipulation to add paths.
This leads to an anti-pattern of extra code in all notebooks where you must use outside-of-local-path files.
This repository demonstrates how to use a simple file and related configuration found within .vscode/settings.json
to configure VS Code Jupyter notebooks so that they use the root of the repository as their root.
This enables notebooks to open and automatically use the root of the repository as their root and avoids the addition of sys.path
manipulation within each notebook.
This work requires uv
to install the environment.
Any environment manager can be used to reproduce the results (uv
was just used to build a quick example).
- Run
uv sync
to synchronize theuv
environment locally. - Explore the local Python package
cow
and modulesay
to better understand how they're structured in context with the notebook found withinsrc/notebooks/
. - Open the notebook under
src/notebooks/example.ipynb
within VS Code. - Within the VS Code notebook window, select kernal:
demo-vs-code-notebook-root
found under.venv/bin/python
. - Run the notebook to reproduce the local pathing result.