Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Jupyter notebook use PYTHONPATH in system variables without hacking sys.path directly?

Same problem as in this question sys.path different in Jupyter and Python - how to import own modules in Jupyter?. In pure Python, it prepends my system environment variable PYTHONPATH to sys.path but Jupyter notebook doesn't, so I can't import my own module.

There are many similar questions asked on SO, and the solution is to directly manipulate sys.path in the script.

Is there a way to make Jupyter notebook use my system PYTHONPATH variable, as in pure python?

like image 433
jf328 Avatar asked May 15 '17 17:05

jf328


People also ask

Is Pythonpath same as SYS path?

PYTHONPATH is related to sys. path very closely. PYTHONPATH is an environment variable that you set before running the Python interpreter. PYTHONPATH , if it exists, should contain directories that should be searched for modules when using import .


1 Answers

JupyterLab reuses the PYTHONPATH on Linux, so I created a file like

#!/bin/bash
# add your path
export PYTHONPATH="$PYTHONPATH:/opt/your/path"
# start JupyterLab using an environment
/opt/anaconda/envs/MY_ENVIRONMENT/bin/jupyter-lab

saved it as start_my_jupyterlab, make it executeable with chmod a+x start_my_jupyterlab and run it on the shell with start_my_jupyterlab.

like image 51
tardis Avatar answered Oct 22 '22 20:10

tardis