Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter can't find keras' module

I have installed Tensorflow and Keras by Anaconda (on Windows 10), I have created an environment where I am using Python 3.5.2 (the original one in Anaconda was Python 3.6). When I try to execute import keras as ks, I get ModuleNotFoundError: No module named 'keras'.

I have tried to solve this issue by sys.path.append(C:\\Users\\ ... \\Anaconda3\\python.exe)

with both notebook and console, but I continue to get the same error.

How could I solve this issue?

like image 858
Simone Avatar asked Apr 22 '17 10:04

Simone


People also ask

Is keras available in Jupyter Notebook?

You have to do !pip install keras within your jupyter notebook to install the keras package before you can import keras. Keras uses tensorflow backend, so when you install keras it installs tensorflow as part of the requirements.


2 Answers

Please try the following:

Run these in the jupyter notebook cell:

import sys

sys.path

sys.executable

It may not be pointing to your virtual environment but to the root

The fix is to install the jupyter notebook from inside your virtual environment

$ . your_env/bin/activate

(your_env)$ python -m pip install jupyter

Now you can import tensorflow or keras

like image 65
sandeep srivastava Avatar answered Oct 18 '22 22:10

sandeep srivastava


Jupyter uses iPython under the hood, for python. So when you install Jupyter, it will also install iPython. There was one issue when I installed keras and Jupyter: I already have iPython installed in my root Anaconda environment. This is the output after I install Jupyter and keras:

In [2]: import sys; sys.path
Out[2]: 
['/home/user/anaconda3/bin',
 '/home/user/anaconda3/lib/python36.zip',
 '/home/user/anaconda3/lib/python3.6',
 '/home/user/.ipython']

Notice that even though I am inside my conda environment, it still looks for libraries in my root conda environment. And of course keras isn't there.

The step to fix is simply re-activate my environment, with:

source deactivate && source activate [my_env]

Then I am using a correct ipython:

Out[2]: 
['/home/user/anaconda3/envs/ml3/bin',
 '/home/user/anaconda3/envs/ml3/lib/python36.zip',
 '/home/user/anaconda3/envs/ml3/lib/python3.6',
 '/home/user/.ipython']
like image 33
bizi Avatar answered Oct 18 '22 22:10

bizi