Select Page

How to Solve ModuleNotFoundError: No module named ‘tensorflow.contrib’

by | Programming, Python, Tips

A common error you may encounter when using Python is modulenotfounderror: no module named ‘tensorflow.contrib’. This error occurs because tensorflow.contrib is deprecated for TensorFlow 2.0.

You can either find the submodule under tensorflow.contrib and see where it has moved. Or you can revert to a specific TensorFlow version using pip or conda and import the contrib submodule.

This tutorial goes through the exact steps to troubleshoot this error with code examples.

ModuleNotFoundError: No module named ‘tensorflow.contrib’

Let’s look at an example where we want to use TensorFlow slim. We can try to install and use slim with the following command:

import tensorflow.contrib.slim as slim
ModuleNotFoundError                       Traceback (most recent call last)

----≻ 1 import tensorflow.contrib.slim as slim

ModuleNotFoundError: No module named 'tensorflow.contrib'

We get this error because the contrib submodule is deprecated for TensorFlow 2.0, the latest TensorFlow version.

Many classes and submodules were either put under tf.compat.v1 like Eager Execution or became separate installable modules like Tensorflow Slim.

If you have written code with TensorFlow 1.x and upgraded to TensorFlow 2.0, you can follow the TensorFlow tutorial for migrating from Tensorflow 1.x to TensorFlow 2.

Solution #1: Install TF Slim

Install Using Pip

You can install pip by downloading the installation package, opening the command line and launching the installer. You can install pip by running the following command in the directory that contains the script.


Then you can install tf_slim using pip using the following command:

pip install --upgrade tf_slim

Then you can import TF Slim using:

import tf_slim as slim

Solution #2: Install a Specific Version of Tensorflow

You can revert to an older version of TensorFlow to access the contrib submodule. Note that if you go to an earlier version, there may not be continued support for that version, and you may encounter compatibility issues with other modules you want to use, like NumPy. It is always better to update to TensorFlow 2.0 and do the necessary migration of your code. Let’s go through how to install a specific version of TensorFlow with Anaconda:

Create a Virtual Environment Using Anaconda

First, create the conda environment with the following command:

conda create -n tfcontrib python=3.6

Then activate the environment:

conda activate tfcontrib

You should see “tfcontrib” in parenthesis next to your command line prompt.

You can search for all installable versions of TensorFlow using conda search:

conda search tensorflow

This command will print a list of all installable versions. To install a specific version, you need to do:

conda install tensorflow==1.14.0

This version has the contrib submodule. Once you have installed this version of TensorFlow, you can import slim using the following statement:

import tensorflow.contrib.slim as slim


Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you are trying to use a submodule of a module and you cannot import it, you can search for it in the module’s documentation.

The submodule may have moved to a separate module like tf_slim with TensorFlow 2.0. To use the submodule, you can either install the new module or revert to an earlier version of the parent module that has the submodule.

TensorFlow 2.0 is a major version upgrade, and there is documentation to help you migrate your code from 1.x to 2.0. Migrating your code will help avoid future import errors with TensorFlow.

For further reading on TensorFlow, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!