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.
Table of contents
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.
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 get-pip.py 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:
- How To Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’.
- How to Convert a Tensor to a Numpy Array in TensorFlow
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!