How to set the default working directory of all Jupyter Notebooks as the project's parent folder

Viewed 770

Question: How can one set the working directory of all notebooks opened in Jupyter Lab with a double-click, to be the project's folder, /myproject/, regardless of the notebook's subfolder within that parent folder? The working directory is identified by !pwd on Linux/Mac or !cd on Windows.

Context: The Jupyter Lab sessions is initiated from the project's folder, by: /myproject/jupyter lab.

I am not looking for changing the working directory within the notebook with code (e.g. with !cd.. or using os), but change the settings of Jupyter Lab, such that the all kernels will start with the folder from where Jupyter Lab was initiated.

This is useful for being able to use consistent relative folders, regardless of the subfolder they are referred from. For example, for loading data in a specific subfolder.

1 Answers

I agree that this is often a preferred approach! I always have my notebooks configured to work like that because it:

  • makes it easy to specify paths to data and for outputs and
  • allows moving a notebook between directories without the need to change the paths
  • makes jupyterlab-lsp code intelligence work more reliably

I have a python module called make_paths_absolute.py with the following contents:

from os import chdir
from pathlib import Path


def make_paths_relative_to_root():
    """Always use the same, absolute (relative to root) paths

    which makes moving the notebooks around easier.
    """
    top_level = Path(__file__).parent

    chdir(top_level)


make_paths_relative_to_root()

And in the first cell of every notebook, I add a line import make_paths_absolute. I like it this way because it makes it:

  • reproducible: anyone who copies/clones my project will be able to run the code without the need to customize anything in their Jupyter environment
  • work with all notebook interfaces (whether JupyterLab/RetroLab/classic Notebook)
  • is quite obvious for anyone reading the notebook that they should expect the paths to be absolute (=relative to the root).

To make that work you first need to set PYTHONPATH pointing to the root of the repository when starting JupyterLab. To do so on Linux/Mac you can prepend the jupyter lab command with:

PYTHONPATH=/path/to/your/lab/root:$PYTHONPATH jupyter lab

PS. The above is a simplification of my setup. In reality, I store make_paths_absolute.py in helpers package (so there is also an empty helpers/__init__.py and there is extra .parent) together with other initialization-time code, including a notebook called notebook_setup.ipynb which contains things like %matplotlib inline, pandas customizations (like making sure it uses stable sort), warning filters etc. So my every notebook starts with:

import helpers.make_paths_absolute
%run helpers/notebook_setup.ipynb

and I am really liking this setup working like that for two years now without any problems.

There is a feature request for making this easier at: https://github.com/jupyterlab/jupyterlab/issues/11619.

Related