Jupyter Notebook in virtual environment doesn't see the virtual env packages

南楼画角 提交于 2019-12-19 05:24:19

问题


I'm trying to use a Jupyter Notebook in a virtual environment. I have created a new virtualenv virtualenv ker12 + activate + installed a specific version of keras or any other library.

also as mentioned in Using a virtualenv in an IPython notebook I did:

pip install ipykernel

and

python -m ipykernel install --user --name=my-virtualenv-name

when I run the notebook and write ! which jupyter the output is correct

/Users/myname/virtualenv/ker12/bin/python

but when I try to import a library, for example import keras there is an error.

ImportError: No module named keras

But from the other side when I write pip freeze | grep Keras the output is:

Keras==1.2.0

UPDATE 1:

this problem is not related to Keras it occurs with any other library (for example pandas)

If I print os.path the output is following:

<'module 'posixpath' from /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc>

From a "command line python" the os.path looks correct

<'module 'posixpath' from '/Users/my_name/virtualenv/ker12/lib/python2.7/posixpath.pyc'>

UPDATE 2:

If I print sys.path from terminal and jupyter the output is also different:

from terminal

/Users/myname/virtualenv/ker12/lib/python27.zip /Users/myname/virtualenv/ker12/lib/python2.7 /Users/myname/virtualenv/ker12/lib/python2.7/plat-darwin /Users/myname/virtualenv/ker12/lib/python2.7/plat-mac /Users/myname/virtualenv/ker12/lib/python2.7/plat-mac/lib-scriptpackages /Users/myname/virtualenv/ker12/lib/python2.7/lib-tk /Users/myname/virtualenv/ker12/lib/python2.7/lib-old /Users/myname/virtualenv/ker12/lib/python2.7/lib-dynload /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /Users/myname/virtualenv/ker12/lib/python2.7/site-packages

from JUPYTER

/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /usr/local/lib/python2.7/site-packages /usr/local/lib/python2.7/site-packages/IPython/extensions /Users/myname/.ipython `


回答1:


the solution is to open jupyter notebook with following command:

~/virtualenv/my_venv_name/bin/jupyter-notebook




回答2:


You should not install ipykernel - instead, you should go for a full Jupyter installation (pip install jupyter) inside your virtual environment. Additionally, be sure that you don't create your virtual environment with the --system-site-packages option.

See also this answer.



来源:https://stackoverflow.com/questions/46051998/jupyter-notebook-in-virtual-environment-doesnt-see-the-virtual-env-packages

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