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

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-18 03:39:39

问题


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:


Jupyter uses its own JUPYTER_PATH environment variable.




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




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



来源:https://stackoverflow.com/questions/43985787/how-to-make-jupyter-notebook-use-pythonpath-in-system-variables-without-hacking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!