How to Install and Import Keras in Anaconda/Jupyter Notebooks

Do you work in Jupyter Notebooks and have an issue in installing and hence importing Keras? Well, you are at the right place.

I was in the same boat a few days back. I struggled for a few hours and could not get a breakthrough and gave up that day. The next day, I again started with a different approach and it clicked!

Just a disclaimer I work on Mac OSx Sierra(10.12.6) and this post is all about installing Keras and importing keras in Jupyter Notebook.

Installing Keras in Anaconda

So, first I did what I usually do to install any library.

pip install keras 

But, it did not actually work. When I tried to import keras in my Jupyter Notebook, I got the below error:

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

So, what I did next is to try installing tensorflow as per the error message. Guess what? I got another error:

ERROR: Cannot uninstall ‘wrapt’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

I tried uninstalling and then re-installing and keep on getting some error or another.

So, I did a couple of search in google and tried the below suggestions:

conda update wrapt
pip install tensorflow
pip install tensorflow-gpu==1.14
pip install tensorflow ==1.14

No! Nada! Nothing worked!

But finally, I got a solution which actually worked and it is simple! Stay tuned!

A Working Solution:

Step 1: Create a new environment

Open the terminal and create a new environment. I have named my environment “keras_env“.

conda create --name keras_env
Step 2: Activate the environment

Now, activate the environment created above.

conda activate keras_env
Step 3: Install keras

To install keras, we need to type the below command:

conda install -c anaconda keras

It will take some time to install.

After analyzing, it will show a list of packages to be installed and will ask for a confirmation to proceed. Press Y to continue.

Step 5: Import Keras in Jupyter Notebook

Once the installation is complete, open Anaconda Environments. The new environment created above should be there. For me, it is called “keras_env“. Now, search for the library Keras in the new environment. It should be right there if everything goes well.

It should have also installed tensorflow.

Now, go back home and check if the “Applications on” is set to the new environment. For me, it is keras_env.

Finally, you are all set to open the Jupyter Notebook. So, when I clicked on Jupyter Notebook, it took some time to install first, and then it opened. You may get a message like below in Anaconda.

Once the Jupyter Notebook is open, import keras and Voila!

It took so much time to install and import keras that I totally forgot why I was even trying to import Keras!

Happy Learning and Happy Coding!

Please clap once if this post actually solve your problem.

Thank You!

428

8 comments

  1. Anandan Subramani

    Thank you very much. keras_env works. It imports following models successfully:
    import tensorflow as tf
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D

    However, I am running into another issue. When I import pandas or numpy or sklearn it fails. In other words, I can import only keras, not the models in standard base environment

    So, I created a new environment called ‘combo_env’ and pushed both keras and base into it, here is how:
    (keras_env) python -m ipykernel install –user –name=combo_env
    activate base
    (base) python -m ipykernel install –user –name=combo_env
    now in Jupyter notebook under comb_env only standard modules like pandas, numpy, sklearn work, not keras

    Could you please help, how to create a kernel where keras and other standard regular models can be imported

    Thanks

  2. Badrul

    Note: Due to versioning issues I had to use Tensorflow’s version of Keras:

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    from tensorflow.keras.layers import Dropout
    from tensorflow.keras.layers import LSTM

Leave a Reply

Your email address will not be published. Required fields are marked *