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 `

4
  • Try pip freeze -l (list packages local to venv) — is Keras still there?
    – phd
    Sep 5, 2017 at 10:02
  • yes Keras appears for pip freeze -l also
    – Michael D
    Sep 5, 2017 at 12:50
  • try to use the full python/jupyter paths. see this answer
    – uzadude
    Nov 8, 2017 at 11:16
  • God, what a convoluted mess Python is. Jun 13, 2022 at 21:41

2 Answers 2

27

the solution is to open jupyter notebook with following command:

~/virtualenv/my_venv_name/bin/jupyter-notebook

3
  • to me it says: bash: /home/pal/virtualenv/env1/bin/jupyter-notebook: No such file or directory
    – zabop
    Jan 24, 2019 at 12:47
  • did you check, that you indeed have this directory?
    – Michael D
    Jan 26, 2019 at 16:53
  • this didn't work for me, still giving me a ModuleNotFoundError
    – WalksB
    Sep 14, 2020 at 18:06
7

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.

3
  • @deserhaut, also I added additional details [UPDATE], which may bring "more light" on the problem source.
    – Michael D
    Sep 6, 2017 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 7, 2017 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 9, 2017 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.