Last updated: Apr 10, 2024
Reading time·5 min
The "ImportError: cannot import name 'pad_sequences' from
'keras.preprocessing.sequence'" occurs because the keras
module has been
reorganized.
To solve the error, import pad_sequences
from keras.utils
.
ImportError: cannot import name 'pad_sequences' from 'keras.preprocessing.sequence' (/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/keras/preprocessing/sequence.py)
Replace the following import statement.
# ⛔️ Incorrect import from keras.preprocessing.sequence import pad_sequences
With the following import statement.
# ✅ Correct import from keras.utils import pad_sequences print(pad_sequences)
The keras
module has been restructured and now you can also import
pad_sequences
from keras_preprocessing.sequence
.
# ✅ Correct import from keras_preprocessing.sequence import pad_sequences print(pad_sequences)
If you use tensorflow
, make sure to import
pad_sequences
from tensorflow.keras.utils
.
# ✅ Correct import from tensorflow.keras.utils import pad_sequences print(pad_sequences)
keras
is fully integrated into tensorflow
, so importing from keras
directly causes the error.tensorflow.keras
insteadYou might also have to change your other direct imports from keras
to import
from tensorflow.keras
. Here are some examples.
# ⛔️ Incorrect imports import keras from keras import layers from keras.preprocessing import image
Replace them with import statements that import from tensorflow.keras
.
# ✅ Correct imports from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.preprocessing import image
keras
package is bundled in tensorflow
starting version 2.0.0
.If the error persists, try to upgrade your versions of tensorflow
and keras
.
pip install tensorflow keras --upgrade pip3 install tensorflow keras --upgrade # 👇️ For Anaconda conda install -c conda-forge tensorflow conda install -c conda-forge keras # 👇️ For Jupyter Notebook !pip install tensorflow keras --upgrade
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py
and run the file with python main.py
to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# 👇️ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # 👇️ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following command.
pip freeze > requirements.txt
If the error persists, follow the instructions in my ModuleNotFoundError: No module named 'tensorflow' article.
The "AttributeError: module 'keras.preprocessing.image' has no attribute
'load_img'" occurs because the keras
preprocessing API has been deprecated.
To solve the error, import the load_img()
function from
tensorflow.keras.utils.load_img
.
Replace the following import statement.
# ⛔️ Deprecated API from keras.preprocessing import image
With an import from tensorflow.keras.utils
.
# ✅ Correct API from tensorflow.keras.utils import load_img img = load_img( 'thumbnail.webp', target_size=(300, 600), color_mode="grayscale" ) img.show()
Alternatively, you can import the entire tensorflow module.
import tensorflow as tf img = tf.keras.utils.load_img( 'thumbnail.webp', target_size=(300, 600), color_mode="grayscale" ) img.show()
keras
is fully integrated into tensorflow
, so import keras
from the tensorflow
module.The keras
package is bundled in tensorflow
starting version 2.0.0
.
You might also have to change your other direct imports from keras
to import
from tensorflow.keras
. Here are some examples.
# ⛔️ Incorrect imports import keras from keras import layers
Replace them with import statements that import from tensorflow.keras
.
# ✅ Correct imports from tensorflow import keras from tensorflow.keras import layers
You should only import the
load_img
function from tensorflow.keras.utils
as shown in the docs.
tensorflow
and keras
If the error persists, try to upgrade your versions of tensorflow
and keras
.
pip install tensorflow keras --upgrade pip3 install tensorflow keras --upgrade # 👇️ For Anaconda conda install -c conda-forge tensorflow conda update tensorflow conda install -c conda-forge keras conda update keras # 👇️ For Jupyter Notebook !pip install tensorflow keras --upgrade
Make sure to update any other import statements that import directly from
keras
with import statements that import from tensorflow.keras
if you
encounter any other issues.
The "ImportError: cannot import name 'load_img' from
'keras.preprocessing.image'" error occurs because the keras
preprocessing API
has been deprecated.
To solve the error import load_img
from tensorflow.keras.utils
instead.
ImportError: cannot import name 'load_img' from 'keras.preprocessing.image' (/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/keras/preprocessing/image.py)
Replace the following import statement.
# ⛔️ Incorrect import from keras.preprocessing.image import load_img
With an import from tensorflow.keras.utils
.
# ✅ Correct import from tensorflow.keras.utils import load_img print(load_img)
keras
is fully integrated into tensorflow
, so import keras
from the tensorflow
module.You might also have to change your other direct imports from keras
to import
from tensorflow.keras
. Here are some examples.
# ⛔️ Incorrect imports import keras from keras import layers
Replace them with import statements that import from tensorflow.keras
.
# ✅ Correct imports from tensorflow import keras from tensorflow.keras import layers
You should only be importing the
load_img
method from tensorflow.keras.utils
as shown in the docs.
keras
package is bundled in tensorflow
starting version 2.0.0
.tensorflow
and keras
versionsIf the error persists, try to upgrade your versions of tensorflow
and keras
.
pip install tensorflow keras --upgrade pip3 install tensorflow keras --upgrade # 👇️ For Anaconda conda install -c conda-forge tensorflow conda install -c conda-forge keras # 👇️ For Jupyter Notebook !pip install tensorflow keras --upgrade
Make sure to replace the import statements that import from keras
directly
with import statements that import from tensorflow.keras
.
# ✅ correct import from tensorflow.keras.preprocessing.image import ImageDataGenerator # ⛔️ incorrect import from keras.preprocessing.image import ImageDataGenerator
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py
and run the file with python main.py
to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# 👇️ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # 👇️ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following command.
pip freeze > requirements.txt
You can learn more about the related topics by checking out the following tutorials: