25

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?

Community
  • 1
  • 1
jf328
  • 6,841
  • 10
  • 58
  • 82
  • Does this answer your question? [sys.path different in Jupyter and Python - how to import own modules in Jupyter?](https://stackoverflow.com/questions/34976803/sys-path-different-in-jupyter-and-python-how-to-import-own-modules-in-jupyter) – Carl G Mar 02 '20 at 04:34

4 Answers4

9

Use simply the PYTHONPATH.

export PYTHONPATH=/Users/user/my-other-library/
jupyter notebook

I just tested with the newest jupyterlab-2.1.2 and it works.

Marc J. Schmidt
  • 8,302
  • 4
  • 34
  • 33
3

Jupyter uses its own JUPYTER_PATH environment variable.

jf328
  • 6,841
  • 10
  • 58
  • 82
  • 2
    The docs doesn't give an example of how to add to it in the config file. I can't get it to work. Could you give an example of how to add it in jupyter_notebook_config.json, by default looking like: { "NotebookApp": { "nbserver_extensions": { "ipyparallel.nbextension": true } } – mattiasostmar Nov 23 '17 at 16:13
  • Not sure how to do it from config file. The one I had is to make a new system environment variable at where PYTHON_PATH is. – jf328 Nov 23 '17 at 16:24
  • URL is broken now. – Shmalex Dec 02 '22 at 16:27
3

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.

tardis
  • 1,280
  • 3
  • 23
  • 48
  • This worked well for me. Though for my case (on OSX) the conda environments are under `/Users/ME/anaconda3/envs/MY_ENVIRONMENT/bin/jupyter-lab` – Johann Sep 30 '20 at 13:40
2

--Just chiming in here since the accepted answer didn't give the complete solution--

You can add the path to your modules to the JUPYTER_PATH environment variable, just the same as you would for modifying the PYTHONPATH environment variable:

export JUPYTER_PATH="${JUPYTER_PATH}:/path/to/add/here/"

If you're on a Mac or other Unix system, you would just drop the above line into your ~/.bash_profile

Hint: make sure you run source ~/.bash_profile to enact the changes and close and restart your jupyter notebook.

kev8484
  • 638
  • 1
  • 10
  • 17