29

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?

1

5 Answers 5

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OP is clearly asking how to do it inside the notebook.
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.

1 Comment

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
3

Jupyter uses its own JUPYTER_PATH environment variable.

3 Comments

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 } }
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.
URL is broken now.
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.

Comments

0

You can just add the environment variable to the kernel.json file of the used kernel (virtual env):

 ,"env": {
    "PYTHONPATH": "/path/to/folder:$PYTHONPATH"
  }

The kernel.json could be found somewhere in the .local folder (~/.local/share/jupyter/kernels/your_venv/kernel.json)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.