21

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 `

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Michael D
  • 1,711
  • 4
  • 23
  • 38

2 Answers2

27

the solution is to open jupyter notebook with following command:

~/virtualenv/my_venv_name/bin/jupyter-notebook

Michael D
  • 1,711
  • 4
  • 23
  • 38
6

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • @deserhaut, also I added additional details [UPDATE], which may bring "more light" on the problem source. – Michael D Sep 06 '17 at 10:11
  • @MichaelD can you share the results of a `jupyter kernelspec list` command, along with `which jupyter`, both when you are in "main" python and when in the virtual env? – desertnaut Sep 07 '17 at 15:50
  • without virtualenv there is an error > "Error executing Jupyter command 'kernelspec': [Errno 2] No such file or directory" But in virtualenviroment (any of therm) the output is following: `bash /Users/myname/Library/Jupyter/kernels/bash` `ker12 /Users/myname/Library/Jupyter/kernels/ker12` `phi /Users/myname/Library/Jupyter/kernels/phi` `python /Users /myname /virtualenv/ tf_new/ bin/../ share /jupyter/kernels / python2 ` @desertnaut – Michael D Sep 09 '17 at 19:10