Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import keras after installation

I'm trying to setup keras deep learning library for Python3.5 on Ubuntu 16.04 LTS and use Tensorflow as a backend. I have Python2.7 and Python3.5 installed. I have installed Anaconda and with help of it Tensorflow, numpy, scipy, pyyaml. Afterwards I have installed keras with command

sudo python setup.py install

Although I can see /usr/local/lib/python3.5/dist-packages/Keras-1.1.0-py3.5.egg directory, I cannot use keras library. When I try to import it in python it says

ImportError: No module named 'keras'

I have tried to install keras usingpip3, but got the same result.

What am I doing wrong? Any Ideas?

like image 865
nabroyan Avatar asked Oct 08 '16 09:10

nabroyan


People also ask

Why keras is not working in Python?

Conclusion – This error (no module named keras) occurs only when either keras is not installed or its path is not properly set.

Does Python 3.8 support keras?

Installation & compatibility To start using Keras, simply install TensorFlow 2. Keras/TensorFlow are compatible with: Python 3.7–3.10.


2 Answers

Ran to the same issue, Assuming your using anaconda3 and your using a venv with >= python=3.6:

python -m pip install keras sudo python -m pip install --user tensorflow 
like image 22
Beltino Goncalves Avatar answered Sep 20 '22 19:09

Beltino Goncalves


Diagnose

If you have pip installed (you should have it until you use Python 3.5), list the installed Python packages, like this:

$ pip list | grep -i keras Keras (1.1.0) 

If you don’t see Keras, it means that the previous installation failed or is incomplete (this lib has this dependancies: numpy (1.11.2), PyYAML (3.12), scipy (0.18.1), six (1.10.0), and Theano (0.8.2).)

Consult the pip.log to see what’s wrong.

You can also display your Python path like this:

$ python3 -c 'import sys, pprint; pprint.pprint(sys.path)' ['',  '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',  '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5',  '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',  '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',  '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages'] 

Make sure the Keras library appears in the /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages path (the path is different on Ubuntu).

If not, try do uninstall it, and retry installation:

$ pip uninstall Keras 

Use a virtualenv

It’s a bad idea to use and pollute your system-wide Python. I recommend using a virtualenv (see this guide).

The best usage is to create a virtualenv directory (in your home, for instance), and store your virtualenvs in:

cd virtualenv/ virtualenv -p python3.5 py-keras source py-keras/bin/activate pip install -q -U pip setuptools wheel 

Then install Keras:

pip install keras 

You get:

$ pip list Keras (1.1.0) numpy (1.11.2) pip (8.1.2) PyYAML (3.12) scipy (0.18.1) setuptools (28.3.0) six (1.10.0) Theano (0.8.2) wheel (0.30.0a0) 

But, you also need to install extra libraries, like Tensorflow:

$ python -c "import keras" Using TensorFlow backend. Traceback (most recent call last):   ... ImportError: No module named 'tensorflow' 

The installation guide of TesnsorFlow is here: https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html#pip-installation

like image 132
Laurent LAPORTE Avatar answered Sep 21 '22 19:09

Laurent LAPORTE