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

前端 未结 4 1450
难免孤独
难免孤独 2020-12-31 05:42

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

相关标签:
4条回答
  • 2020-12-31 06:07

    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.

    0 讨论(0)
  • 2020-12-31 06:10

    Jupyter uses its own JUPYTER_PATH environment variable.

    0 讨论(0)
  • 2020-12-31 06:12

    --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.

    0 讨论(0)
  • 2020-12-31 06:20

    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.

    0 讨论(0)
提交回复
热议问题