[Solved] No Module Named Tensorflow Contrib

As we know that the scope of the python programming language is relatively high, and it is used in different domains relentlessly. It is either machine learning, deep learning, or web development. It is proven one of the developer’s favorite. Its vast popularity lies in the fact that it has several libraries that support work in different domains. Some of them are Scipy, TensorFlow, PyTorch, e.t.c. While using TensorFlow, many people encounter problems like No Module Named Tensorflow Contrib.

Now, no module named TensorFlow contrib arises when the interpreter cannot find the ‘contrib’ module in the TensorFlow library. So, before going to the solution of the error, let’s see its root cause first.

What is No Module Named Tensorflow Contrib?

import tensorflow.contrib.slim as slim

Output:

Traceback (most recent call last):
File "/home/toshiba/PFE/DocFace-master/src/tflib.py", line 28, in <module>
import tensorflow.contrib.slim as slim

ModuleNotFoundError: No module named 'tensorflow.contrib'

So, the error suggests that the interpreter cannot locate any module named contribs in the TensorFlow package. Subsequently, it will not find or use all the modules, classes, or methods available in TensorFlow.contrib package. In similar scenarios, the issue is usually resolved by reinstalling the module as there may be some path mismatch or incorrect path reference. But, when this issue goes with TensorFlow, the reason is slightly different.

Why do I get ” No Module Named Tensorflow Contrib ” Error?

Now, the reason for the error is that many of the modules and packages are deprecated in Tensorflow version 2.x. from TensorFlow version 1.x. In the older version, many customs APIs are created in TensorFlow.contrib files which make the integration of TensorFlow functionalities more difficult. Looking at these scenarios, the TensorFlow community decided to make appropriate changes for the seamless experience of the users. The updated version of TensorFlow tried to remove all unnecessary code APIs in the package and moved applicable packages into new modules. This leads to straightforward access to the classes available in the TensorFlow package.

Solutions To “No Module Named Tensorflow Contrib”

Solution 1:- Finding location of new packages

Now the solution to the error is platform-independent. You need to figure out the new packages supported by the TensorFlow version 2.x and re-write your imports. This solution may be a little tedious to you, but it is the standard way to maintain your code. Like it is time for you to enjoy TensorFlow with new functionalities and capabilities. For this, you need to find out the new locations of the packages in Tensorflow version 2 that you want to use from version 1. After locating them, you can easily import them into your code. Moreover, some packages (like tensorflow_addons) may require a separate installation in the system for use, but things get easy once done. You can find the list of packages with their new location on the following website.

Please go through this link to learn more about them.

[Note:- There are many packages that are deleted and are not moved in version 2.x. Moreover, there are many packages that are currently under consideration. So, you might feel difficulty in using them. You may try the second solution discussed below for them.]

Solution 2:- Reinstallation to Older version

The other solution can be to use TensorFlow version 1.x in your code. For that, uninstall TensorFlow 2.x and then reinstall it with version 1.x. To do that, use the following command.

pip uninstall tensorflow
pip3 install tensorflow==1.14.0 

Now, this is not recommended but can be used when you have no way. However, this method also creates some new problems like incompatibles of TensorFlow with other packages, e.t.c. So try to avoid using this.

No module named ‘tensorflow.contrib’ collab

When one works with Google collaboratory, they may face the same error there. The reason for the error is similar as mentioned above. But it is easier to maintain things in google collab. Google collab comes with two preinstalled versions of Tensorflow, i.e., 1.x and 2.x. The default version is set to 2.x. However, if one wants to work on version 1.x and its compatibility, they can easily do that. Use the following command to import an older version of TensorFlow.

%tensorflow_version 1.x

Once you successfully switch to the older version of TensorFlow, you can use TensorFlow.contrib module on google collab.

[Note:- You are still recommended to use the latest version of TensorFlow and follow the solution as advised in solution 1 in the above section of the article.]

Conclusion

So, today in this article, we have discussed the possible reasons for the error no module named TensorFlow contrib. After that, we discussed the possible solutions and the necessary changes we needed to make to run our code.

I hope this article has helped you. Thank You.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments