Installed Keras with pip3, but getting the "No Module Named keras" error

11,485

Solution 1

Installing Anaconda and then install packages with pip seams like confusing the goal of Anaconda(or any other package management tools)

Anaconda is there to help you organize your environments and their dependences.

Assuming you have conda on your system path, Do:

Update conda

conda update conda

We can create an environment called 'awesome' with python 3.6 and add all awesome datascience packages coming with anaconda(numpy, scipy, jupyter notebook/lab etc), and tensorflow and keras. you can drop anaconda and have minimal package if desired.

conda create -n awesome python=3.6 anaconda tensorflow keras

After quite a time, and all is well, activate your environment and test if we can import keras.

conda activate awesome
python -c "import keras"

When done doing awesomeness, you can deactivate as so:

conda deactivate

conda is better than pip because it deal with libraries compatibalities. It upgrades and downgrade packages for you.

Sometimes beautiful about Anaconda is that you can just install the main package and it will install all its dependences for you, so you could just do:

conda create -n awesome python=3.6 keras

This will automatically find all packages that keras depends on or set to default such as tensorflow and numpy

What you are doing wrong:
You get that error because your python sys.path can not locate the packages you install.

You can do:

python -c "import sys;print(sys.path)"

This will print the location your python will look for packages. It is most likely that the path to keras library is not one them.

When you just use pip to install, your default python that has that pip will have access to your installations. So if you have multiple Pythons, the recommendation is to be explicit like:

python3 -m pip install packages 

So here you are sure that it is Python in python3 directory that did installation. This is where we need environments that keeps our Python versions and dependence different and easy to control. Anaconda, Pipenv, Poetry, piptools and more are there trying to help you managed your systems better ;)

Update: For Jupyter Notebook/Lab users

If you already have Jupyter, say on your base environment, we can add awesome as another kernel:

conda activate awesome 
(awesome ) conda install ipykernel -y
(awesome) python -m ipykernel install --user --name my_env --display-name "Awesome"
conda deactivate

Now if you run Jupyter, you should be able to choose between Base Python and Awesome environment.

Solution 2

It sounds like you may have used pip3 to install while having multiple python installations on your machine.

Did you happen to have python installed on your machine before install Anaconda? Sometimes the pip3 in your PATH variable is for a different version than Anaconda.

Try this, and then run your code again:

conda install keras

Run conda list to see if it is installed in your Anaconda python installation:

conda list

Update

If it is still not working, then try this:

\path\to\Anaconda\python\python3 -m pip3 install keras

This uses pip3 but ensures that is selecting the correct installation when installing keras.

Solution 3

It helped me to first check on the command line to see whether Keras was indeed installed as noted in a previous answer here.

python -c "import keras"

If you get an error with that command, you probably haven't installed keras in the right environment.

After going through a number of solutions, I was still getting the error. Turns out I had to restart my jupyter notebook for the changes to take effect - just in case you're using notebook.

Share:
11,485
ABCD
Author by

ABCD

Updated on June 26, 2022

Comments

  • ABCD
    ABCD almost 2 years

    I am Creating a leaf Identification Classifier using the CNN, the Keras and the Tensorflow backends on Windows. I have installed Anaconda, Tensorflow, numpy, scipy and keras.

    I installed keras using pip3:

    C:\> pip3 list | grep -i keras
    Keras               2.2.4
    Keras-Applications  1.0.6
    Keras-Preprocessing 1.0.5
    

    However, when i run my project i get following error

    ModuleNotFoundError: No module named 'keras'
    

    Why is the module not found, and how can I fix this error?

  • ABCD
    ABCD over 5 years
    Yes i installed python on my machine before install Anaconda .I try your code .still not working.
  • Daniel Scott
    Daniel Scott over 5 years
    @kajasumanie kanapathipillai I am convinced that it is related. Can you paste the stack trace you are getting here if it's different than 'ModuleNotFoundError: No module named 'keras' '? And please also check if its in the ouput when you try: conda list in the shell.
  • ABCD
    ABCD over 5 years
    it's 'ModuleNotFoundError: No module named 'keras'
  • Daniel Scott
    Daniel Scott over 5 years
    @kajasumanie kanapathipillai And did you see it in the list when you tried 'conda list' in the console?
  • Daniel Scott
    Daniel Scott over 5 years
    @kajasumanie kanapathipillai Take a look at the update I just made to the solution. Give it a try and let me know how it goes. I'm sure it will work for you. :)
  • Regi Mathew
    Regi Mathew over 5 years
    I could solve the conda issue I was facing (condaupgradeerror) through this elegant approach. Thanks very much.
  • efueyo
    efueyo almost 5 years
    Following this instructions, I created the virtual environment "awesome", installing in python 3.6, tensorflow and keras. Subsequently I have listed all the packages loaded in the environment and verified that both are in it. However, when trying to import tensorflow and keras in a jupyter notebook, it returns the error "no module name ..." What could be my problem?
  • Prayson W. Daniel
    Prayson W. Daniel almost 5 years
    Your Jupyter does not have access to your awesome environment. Update answer for Jupyter part