Rhea Mae Perito
Rhea Mae Perito

Reputation: 421

Google Colab error: Import "tensorflow.keras.models" could not be resolved(reportMissingImports)

import tensorflow as tf
tf.__version__

!sudo pip3 install keras

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator

Error message:

Import "tensorflow.keras.models" could not be resolved(reportMissingImports)
>Import "tensorflow.keras.layers" could not be resolved(reportMissingImports)
>>Import "tensorflow.keras.preprocessing.image" could not be resolved(reportMissingImports)

Upvotes: 31

Views: 89876

Answers (9)

Konstantin
Konstantin

Reputation: 1280

In case somebody faces the same issue in 2024+ with new versions of tensorflow and keras now the paths should be like this

from keras.api.layers import *
from keras.api.optimizers import *

They have been moved to api subdirectory

Upvotes: 3

Kalidas VijayBhak
Kalidas VijayBhak

Reputation: 19

Using this command solved my issue It is the only command you need!!!

I tested all the methods but it didn't work finally this works

!sudo pip3 install keras

Upvotes: -1

Brend8c
Brend8c

Reputation: 649

I am using VScode, this is how I managed to solve the problem in python script

from tensorflow.python.keras.models import Sequential, load_model
from tensorflow.python.keras.layers import LSTM, Dense

Upvotes: 13

theneekz
theneekz

Reputation: 137

I believe keras is now installed as its own module when you pip install tensorflow, so you can remove the "tensorflow." from your import statements. So if you first:

pip3 install tensorflow

You can access specific models of keras like this:

from keras.models import Sequential

Upvotes: 0

ahzary
ahzary

Reputation: 351

this worked for me.

from tensorflow import keras
from keras.layers import Dense
from keras.models import Sequential, load_model

Upvotes: 35

Hari Krishna
Hari Krishna

Reputation: 11

I solved it.

The problem was my version was too new. You just need to lower your TensorFlow version so that its suitable for your requirement. I've lowered my version from 2.8.0 to 2.7.0 using pip install tensorflow==2.7.0.

Check these images, the warning lines disappeared for me.

Before I downgraded my TensorFlow version

After I downgraded my TensorFlow version

Upvotes: 1

i had the same error , l did those steps step 01 : uninstall the existing tensorflow with this instruction

!pip uninstall tensorflow

step 02 : install this version i hope you find what you need in this version with this instruction

!pip install tensorflow==2.7.0

you can change the version of tensorflow if 2.7.0 not good for your code

Upvotes: 3

Rhea Mae Perito
Rhea Mae Perito

Reputation: 421

Issue solved.

Though the error: Import "tensorflow.keras.models" could not be resolved(reportMissingImports) prompts, it doesn't affect the entire code. My Tensorflow version is 2.8.0. I just found out that the problem is on the config of my cnn model.

Upvotes: 1

Watson21
Watson21

Reputation: 141

import tensorflow as tf
tf.__version__

!sudo pip3 install keras

from tensorflow.python.keras.engine.sequential import Sequential

from tensorflow.python.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D

image_data_generator = tf.keras.preprocessing.image.ImageDataGenerator()

Upvotes: 11

Related Questions