Search code examples
pythonwindowstensorflowkeraskeras-2

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


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?


Solution

  • 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.