Mo.be
Mo.be

Reputation: 95

jupyter notebook can not import keras

I have installed Keras and TensorFlow-GPU but when I try to import these libraries into Jupiter notebook there is an error

Keras-applications        1.0.8                    pypi_0    pypi
keras-preprocessing       1.1.2                    pypi_0    pypi
tensorboard               2.1.1                    pypi_0    pypi
tensorflow-gpu            2.1.0                    pypi_0    pypi
tensorflow-gpu-estimator  2.1.0                    pypi_0    pypi
numpy                     1.19.2                   pypi_0    pypi
opencv-python             4.4.0.44                 pypi_0    pypi
pip                       19.2.3                   py37_0

here are the libraries using conda list . and here is the error that jupyter displays to me :

ModuleNotFoundError Traceback (most recent call last) in ----> 1 import keras 2 from keras.models import Sequential 3 from keras.layers import Dense, Activation 4 import numpy as np 5

ModuleNotFoundError: No module named 'keras'

I try this one in anaconda environment:

pip3 install keras

Requirement already satisfied: keras in c:\users\msi-pc\appdata\local\programs\python\python39\lib\site-packages (2.4.3) Requirement already satisfied: numpy>=1.9.1 in c:\users\msi-pc\appdata\local\programs\python\python39\lib\site-packages (from keras) (1.19.4) Requirement already satisfied: scipy>=0.14 in c:\users\msi-pc\appdata\local\programs\python\python39\lib\site-packages (from keras) (1.5.4) Requirement already satisfied: h5py in c:\users\msi-pc\appdata\local\programs\python\python39\lib\site-packages (from keras) (3.1.0) Requirement already satisfied: pyyaml in c:\users\msi-pc\appdata\local\programs\python\python39\lib\site-packages (from keras) (5.3.1)

I'm grateful if you help me.

P. S : I realized that in order to import keras /tensorflow from the second version on (tensorflow>=2.0.0 ) i have to use import tensorflow.keras And everything will be good.

Upvotes: 1

Views: 5902

Answers (3)

Pranav J
Pranav J

Reputation: 80

I am not sure how you have imported keras, but in past even I have faced a similar issue. What I had done is i did something like this

import keras

which is wrong! We have to import it like this

from tensorflow import keras

which worked fine for me! Hope this Helps you!

Upvotes: 0

Aanshumaan Shrijai
Aanshumaan Shrijai

Reputation: 119

Can you please tell me if you're using multiple versions of python on the same device, if so please check if you've installed TensorFlow on the same version of python which you're using for jupyter notebook, to check that and install again:

  1. Go to the path where you've installed python(which you're using for jupyter notebook) if you've installed anaconda then go to the path where anaconda is installed and follow the procedure.

  2. Go to the site-packages folder in the path of Anaconda or python.

  3. Check if all the dependencies of TensorFlow and TensorFlow are installed there.

  4. If you can't find it then add the current python version to environment variables, see: https://www.javatpoint.com/how-to-set-python-path#:~:text=SETTING%20PATH%20IN%20PYTHON%201%20Right%20click%20on,on%20Ok%20button%3A%209%20Click%20on%20Ok%20button%3A and https://www.geeksforgeeks.org/how-to-setup-anaconda-path-to-environment-variable/

  5. After you've added the current version of python to the environment path variables then follow this link to install TensorFlow: https://www.geeksforgeeks.org/how-to-install-python-tensorflow-in-windows/#:~:text=%20%20%201%20Step%201%3A%20Click%20on,done%20with%20the%20use%20of%20following...%20More%20 and https://machinelearningspace.com/installing-tensorflow-2-0-in-anaconda-environment/

Then again follow step 2 and 3 and if it's still not appearing in the site-packages folder then follow this link: https://www.quora.com/How-can-I-work-with-Keras-on-a-Jupyter-notebook-using-Tensorflow-as-backend for some details(not that helpful)

Also, Try installing Keras by this command:

pip3 install Keras

If you're using one version of python then please check if jupyter and TensorFlow are installed in the same Virtual Environment Please tell me if it works or not.

Upvotes: 0

Andy
Andy

Reputation: 3170

If you're using tensorflow >= 2.0, then import keras using

from tensorflow import keras

Common convention is to import it as kr

Upvotes: 1

Related Questions