How to reference root directory?

I typically have several Juypterlab launchers each with different starting directories (local, networkshare 1,2,3…) as I store RAW data and associated post-processing in relevant directory structures.

I typically keep a processingLib in this root directory and it usually contains specific parsing functions, as a group of tests may have a different acquisition setup (scope channels, power analyser…), while larger re-use calls are added as a git submodule.

> <root>
    └─ some_file.ipynb
    └─ another_file.ipynb
    └─ another_file_PRESENTATION.ipynb
      └─ looksee_file.ipynb
               └─ additional_file.ipynb

For the above example structure I would group suites of tests in their own directories and have one or many notebooks to process and document the postprocessing. At the start of each notebook, after a heading markdown I would typically add a “houskeeping imports” codeblock

#housekeeping imports
import IPython
import numpy as np
import pandas as pd
import sympy as sym
import seaborn as sns
import scipy.signal as signal
from scipy.optimize import curve_fit
from IPython.display import display,HTML,Markdown 
from dataclasses import dataclass

import sys
from ProcessingLib import some_module

This obviously works for TestSuite_1,TestSuite_2 and TestSuite_3 but would fail for TestSuite_3_1 directory and I would need to change the sys.path.insert to: sys.path.insert(0,’… /…’) ( note discourse has changed two dots to three in main body text…)

Is there some ENV that juypyterlab sets which is the absolute path of that I can instead use for some import to provide a consistent housekeeping import block and minimize worrying about the depth of the directory structure being used

1 Like

I described my setup in ipython - How to set the default working directory of all Jupyter Notebooks as the project's parent folder - Stack Overflow. Briefly I have a file called in the root which contains the following code:

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



In the “houskeeping imports” codeblock I always start from:

import make_paths_absolute

And that never let me down :wink:

PS. the above is a simplification, I have make_paths_absolute in helpers/ and I have another .parent to account for that, and I also try to avoid having more than two lines in the houskeeping block by having a dedicated “housekeeping notebook” executed by %run, but the above gives you the gist.

1 Like