How do I run globally installed Jupyter from within virtual environments?

一曲冷凌霜 提交于 2020-05-26 06:11:31

问题


I'm trying to run Jupyter notebooks with a globally installed version of Jupyter from within virtual environments (using virtualenvwrapper, because I want to manage versions of installed packages). And I do not what to use Anaconda.

The problem is when I run jupyter notebook from within the virtualenv, it cannot find the packages installed in the env, it only finds the packages installed globally.

How do I set up Jupyter to check for packages installed within the virtual environment instead of globally?

Here is what I get when I run which python and which jupyter:

globally:

which python  >>> /usr/local/bin/python
which jupyter >>> /usr/local/bin/jupyter

from within virtualenv:

which python  >>> /Users/brianclifton/.virtualenvs/test/bin/python
which jupyter >>> /usr/local/bin/jupyter

running jupyter notebook from within the virtualenv:

which python  >>> /usr/local/bin/python
which jupyter >>> /usr/local/bin/jupyter

Also, here is my .bash_profile:

export VISUAL=vim
export EDITOR="$VISUAL"

export PS1="\\[\[\e[38;5;94m\][\u] \[\e[38;5;240m\]\w:\[\e[m\] \$(__git_ps1 '(%s)')$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
export PATH=/usr/local/bin/python:/usr/local/bin:$PATH

alias ls='ls -GFh'
alias pserv="python -m SimpleHTTPServer"
alias ipynb="jupyter notebook"

export WORKON_HOME=/Users/brianclifton/.virtualenvs
export PROJECT_HOME=/Users/brianclifton/dev
source /usr/local/bin/virtualenvwrapper.sh

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

alias branch='git rev-parse --abbrev-ref HEAD'

function frameworkpython {
   if [[ ! -z "$VIRTUAL_ENV" ]]; then
      PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python "$@"
   else
      /usr/local/bin/python "$@"
   fi
}

回答1:


One possible solution is to prefix your virutalenv's bin directory to your path. This way jupyter will find the virtualenv's libraries. You can do this by running export PATH:`which python`:$PATH after you activate your environment. It would be easy enough to alias.

However, a better solution may be to add this line to the postactivate hook/script. To find the location of this script do ls $WORKON_HOME after activate virtualenvwrapper and edit $WORKON_HOME/<virtualenv_name>/bin/postactivate.




回答2:


Another solution from virtualenv doc

workon test
pip install ipykernel
python -m ipykernel install --prefix=/usr/local --name test-kernel

Then your kernel should appear when you run jupyter from your other virtualenv, and all the packages installed in test would be available from it. Change prefix value according to the doc if you prefer a per-user installation instead of system-wide



来源:https://stackoverflow.com/questions/42232819/how-do-i-run-globally-installed-jupyter-from-within-virtual-environments

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