Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a default sys.path for a Notebook

I have all my .py files inside a folder script and all my IPython-notebooks under a folder named Notebook.

There are multiple cross dependencies for each notebook file on one or more files on script.

Having sys.path.append on top of every notebook seems cumbersome and I am hoping there is a way to add a default lookup path just like we add PYTHONPATH to .bash_profile.

Now I do the following:

import sys
sys.path.append("<path where DeriveFinalResultSet.py exists>)
import DeriveFinalResultSet as drs

I wish to have a setting where I can do the below:

import DeriveFinalResultSet as drs
like image 685
Sreejith Menon Avatar asked Jul 07 '16 03:07

Sreejith Menon


People also ask

How is SYS path set?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.

What is the default location of Jupyter Notebook?

On Linux and other free desktop platforms, these runtime files are stored in $XDG_RUNTIME_DIR/jupyter by default. On other platforms, it's a runtime/ subdirectory of the user's data directory (second row of the table above).

How do I add path to SYS path?

append(mod_directory) to append the path and then open the python interpreter, the directory mod_directory gets added to the end of the list sys. path. If I export the PYTHONPATH variable before opening the python interpreter, the directory gets added to the start of the list.


2 Answers

To avoid "hidden configurations" (i.e. things that aren't in source control/machine-specific) and to maintain a notebook/code separation like you describe, I do something like the below:

code/
    mymodule.py
    mypackage/
        __init__.py

notebooks/
    mynb.ipynb
    mynb2.ipynb
    paths.py   <--- below

In paths.py:

import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parents[1] / 'code'))
# sys.path[0] = str(pathlib.Path(__file__).parents[1] / 'code')

Then in mynb*.ipynb I can happily do:

import paths
import mymodule, mypackage

, etc.

The latter form effectively replaces the import path from the empty-string (current directory) to the "code" directory, which is perhaps a bit cleaner. This makes imports insensitive to using stuff like os.chdir().

like image 116
Nick T Avatar answered Oct 06 '22 09:10

Nick T


I wrote simple bash script which updates the path and launches Jupyter:

#!/usr/bin/env bash

echo "Saving PYTHONPATH"
ORIGINAL_PYTHONPATH=$PYTHONPATH
echo "Prepending package to PYTHONPATH"
export PYTHONPATH="$PWD/:$ORIGINAL_PYTHONPATH"
echo "Starting Jupyter"
jupyter notebook
echo "Reverting to the original PYTHONPATH"
export PYTHONPATH=$ORIGINAL_PYTHONPATH
like image 23
Dror Avatar answered Oct 06 '22 08:10

Dror