4

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.

Ran Feldesh
  • 1,111
  • 11
  • 17

2 Answers2

3

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

Alternatively, you can specify PYTHONPATH in kernel.json (either by modifying the default kernel specification or creating a copy of it first, see https://stackoverflow.com/a/53595397).

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.

krassowski
  • 13,598
  • 4
  • 60
  • 92
  • Many Many Thanks @krassowski ! This is much appreciated, and a great solution. In my case, I am using the [jupytext](https://github.com/mwouts/jupytext) extension to have a synced ipynb and py files for every notebook. This way I can develop with Jupyter Lab, and when ready - Run with `python3` from the root directory of the project, from a CLI. In this use-case, the `make_paths_absolute()`will have to be commented out. I wonder if there is a Jupyter Lab setting that can auto set the path to the root, without adding code to the notebook itself. Thank you for sharing your setup, it is Great :)! – Ran Feldesh Oct 01 '21 at 14:34
  • 1
    I don't think there is a JupyterLab setting for that. I could imagine that ipykernel kernel could allow this on kernel level via config option, but JupyterLab itself does not have authority over how specific kernels/languages interpret paths. Instead of commenting you could add a conditional to only use this in notebook using something from https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook. I also update the answer to remove an extra `.parent` in the simplified example. – krassowski Oct 01 '21 at 14:44
  • Thank you @krassowski. Yes, I am also not familiar a Jupyter-based option, though I'd imagine that Jupyter Lab might be able to start/initialize the ipython kernel from the root folder, and not from the subfolder, following a double-click on a notebook file. Not sure. The option of using code in the precise way you suggest here is the best alternative I know of, if indeed a Jupyter solution is not available. Thank you so much :) – Ran Feldesh Oct 01 '21 at 15:31
  • I added a crucial bit on changing the python path I forgot earlier. – krassowski Oct 25 '21 at 12:06
  • This is very clever, but very ugly and cumbersome. Is there an open feature request for this on the Jupyter Lab issue tracker? – shadowtalker Jul 12 '22 at 02:30
  • Yes: https://github.com/jupyterlab/jupyterlab/issues/11619 – krassowski Jul 12 '22 at 15:59
0

The most easiest solution in my opinion

  • Open Notepad
  • Paste the command "jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10"
  • Save the notepad file with an extension of ".bat" instead of ".txt"
  • Paste the file in which directory you want to initialize your jupyter
  • Double click and open the ".bat" file
  • Jupyter opens with desired directory as base
  • This way you control the jupyter root directory as and when required and don't really have to perform any manual settings

Hope this helps

".bat" file created on Desktop enter image description here ".bat" file double clicked and executed enter image description here jupyter opens with Desktop as the intended base directory enter image description here