Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 `

like image 971
Michael D Avatar asked Sep 05 '17 09:09

Michael D


2 Answers

the solution is to open jupyter notebook with following command:

~/virtualenv/my_venv_name/bin/jupyter-notebook

like image 74
Michael D Avatar answered Oct 31 '22 17:10

Michael D


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.

like image 30
desertnaut Avatar answered Oct 31 '22 19:10

desertnaut