Cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'
  2. Module keras.preprocessing.image has no attribute load_img
  3. Cannot import name 'load_img' from keras.preprocessing.image

# Cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'

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 preprocesing sequence

shell
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.

main.py
# ⛔️ Incorrect import from keras.preprocessing.sequence import pad_sequences

With the following import statement.

main.py
# ✅ Correct import from keras.utils import pad_sequences print(pad_sequences)

import pad sequences from keras utils

The keras module has been restructured and now you can also import pad_sequences from keras_preprocessing.sequence.

main.py
# ✅ 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.

main.py
# ✅ 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.

# Import from tensorflow.keras instead

You might also have to change your other direct imports from keras to import from tensorflow.keras. Here are some examples.

main.py
# ⛔️ Incorrect imports import keras from keras import layers from keras.preprocessing import image

Replace them with import statements that import from tensorflow.keras.

main.py
# ✅ Correct imports from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.preprocessing import image
The keras package is bundled in tensorflow starting version 2.0.0.

If the error persists, try to upgrade your versions of tensorflow and keras.

shell
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

upgrade tensorflow keras versions

If none of the suggestions helped, you can try to upgrade all packages in your environment.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. 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.

shell
# 👇️ 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.

shell
pip freeze > requirements.txt

If the error persists, follow the instructions in my ModuleNotFoundError: No module named 'tensorflow' article.

# Module keras.preprocessing.image has no attribute load_img

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.

attributeerror module keras preprocessing image has no attribute load img

Replace the following import statement.

main.py
# ⛔️ Deprecated API from keras.preprocessing import image

With an import from tensorflow.keras.utils.

main.py
# ✅ 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.

main.py
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.

# Updating the rest of your imports

You might also have to change your other direct imports from keras to import from tensorflow.keras. Here are some examples.

main.py
# ⛔️ Incorrect imports import keras from keras import layers

Replace them with import statements that import from tensorflow.keras.

main.py
# ✅ 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.

# Upgrade your versions of tensorflow and keras

If the error persists, try to upgrade your versions of tensorflow and keras.

shell
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

upgrade tensorflow and keras

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.

# Cannot import name 'load_img' from keras.preprocessing.image

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

shell
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.

main.py
# ⛔️ Incorrect import from keras.preprocessing.image import load_img

With an import from tensorflow.keras.utils.

main.py
# ✅ 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.

# Updating the rest of your import statements

You might also have to change your other direct imports from keras to import from tensorflow.keras. Here are some examples.

main.py
# ⛔️ Incorrect imports import keras from keras import layers

Replace them with import statements that import from tensorflow.keras.

main.py
# ✅ 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.

The keras package is bundled in tensorflow starting version 2.0.0.

# Upgrade your tensorflow and keras versions

If the error persists, try to upgrade your versions of tensorflow and keras.

shell
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.

main.py
# ✅ 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.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
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)
You can store the script in a Python file, e.g. 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.

shell
# 👇️ 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.

shell
pip freeze > requirements.txt

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.