如何在虚拟环境中运行全局安装的Jupyter?

时间:2017-02-14 17:43:33

标签: python ipython virtualenv jupyter-notebook jupyter

我正在尝试在虚拟环境中使用全局安装的Jupyter版本运行Jupyter笔记本(使用virtualenvwrapper,因为我想管理已安装软件包的版本)。而且我不知道如何使用Anaconda。

问题是当我从virtualenv中运行jupyter notebook时,它无法找到env中安装的软件包,它只查找全局安装的软件包。

如何设置Jupyter来检查虚拟环境中安装的软件包而不是全局软件包?

以下是我运行which pythonwhich jupyter时的结果:

全局:

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

来自virtualenv:

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

在virtualenv中运行jupyter notebook

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

另外,这是我的.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
}

2 个答案:

答案 0 :(得分:1)

一种可能的解决方案是将virutalenv的bin目录添加到您的路径前面。这样jupyter会找到virtualenv的库。您可以在激活环境后运行export PATH:`which python`:$PATH来执行此操作。别名很容易。

但是,更好的解决方案可能是将此行添加到postactivate hook/script。要查找此脚本的位置,请在激活virtualenvwrapper并编辑ls $WORKON_HOME后执行$WORKON_HOME/<virtualenv_name>/bin/postactivate

答案 1 :(得分:0)

virtualenv doc

的另一个解决方案
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 16, in swig_import_helper
    fp, pathname, description = imp.find_module('_gdal_array', [dirname(__file__)])
  File "C:\Python34\lib\imp.py", line 297, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_gdal_array'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\dem12002\Desktop\test.py", line 38, in <module>
    rasArray=np.array(src_ds.ReadAsArray())
  File "C:\Python34\lib\site-packages\osgeo\gdal.py", line 1829, in ReadAsArray
    from . import gdalnumeric
  File "C:\Python34\lib\site-packages\osgeo\gdalnumeric.py", line 1, in <module>
    from osgeo.gdal_array import *
  File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 26, in <module>
    _gdal_array = swig_import_helper()
  File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 18, in swig_import_helper
    import _gdal_array
ImportError: No module named '_gdal_array'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\dem12002\Desktop\test.py", line 40, in <module>
    except (RuntimeError,e):
NameError: name 'e' is not defined

然后当您从其他virtualenv运行jupyter时,您的内核应该出现,并且测试中安装的所有软件包都可以从它获得。如果您更喜欢按用户安装而不是系统范围

,请根据文档更改前缀值