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.

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 on 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!

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

conda create --name keras_env

Now, activate the environment created above.

conda activate keras_env

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 in the new environment, 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 solves your problem.

Thank You!

Originally published at https://oindrilasen.com on February 19, 2021.

Data Scientist, Story Teller

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store