pythonkerasvirtualenvjupyter-notebookjupyter

Unable to import Keras in Jupyter


So I set up my virtual lan with python 3 and jupyter (with pip). All works fine but if I try to import keras it will not work in jupyter and tells module not found. But if I execute the same file (python3 test.py) in the terminal it works fine.

which jupyter
/usr/local/bin/jupyter

which python3
/Users/niro273/Desktop/xcorp/bin/python3

If I do a pip3 list these are the results.

jupyter (1.0.0)
jupyter-client (5.1.0)
jupyter-console (5.2.0)
jupyter-core (4.3.0)
Keras (2.0.8)

Note- I have installed jupyter (pip3 install jupyter) inside the virtual env too. So should I have to switch the juypter execution path?


Solution

  • Both Keras & Jupyter must be installed inside your virtual environment; then, you should start Jupyter after activating your virtual env (in which case which jupyter should point to a different location inside your virtual env):

    $ which jupyter
    /usr/local/bin/jupyter
    $ virtualenv /venv/foo
    $ source /venv/foo/bin/activate
    $ (foo) pip3 install jupyter
    $ (foo) which jupyter
    /venv/foo/bin/jupyter
    $ (foo) pip3 install keras
    $ (foo) jupyter notebook
    

    There are certainly other ways (e.g. to install a different kernel in your main Jupyter installation pointing to the Python executable in your foo virtual environment), but I have found the above way to be quicker and more hassle-free, at least for Keras...