Sujan Bal
Sujan Bal

Reputation: 71

How to fix 'No module named 'tensorflow.contrib' for python project?

I found a chatbot program in github and wanted to run this program for my better understanding. But every time I try to run this program, it says

No module named 'tensorflow.contrib'

What should I do to fix this error?

Upvotes: 7

Views: 30165

Answers (4)

jumorap
jumorap

Reputation: 69

Probably you are trying to run it over Windows, try to run it in your terminal:

pip install cloudbiolinux==0.3a

pip install helper

pip install contrib

Upvotes: 0

Yusufmet
Yusufmet

Reputation: 135

Probably the code you found was written in TensorFlow 1.x, but you have TensorFlow 2.x installed. Instead of downgrading TensorFlow, the compatibility module can be used:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

Source:https://www.tensorflow.org/guide/migrate

Upvotes: 3

Pedro Feijo
Pedro Feijo

Reputation: 21

tf.contrib.data has been deprecated and been removed
Try downgrade version of tensorflow:

pip3 install tensorflow==1.14

use venv to install multiple tensorflow version on single machine

Upvotes: 2

swm
swm

Reputation: 111

Explained by other expert: An interesting find, I hope this helps others that are developing under Anaconda or similar integrated environments where your program isn't ran directly from the command line, e.g. like "python myprogram.py".

The problem could be caused by the fact that the program itself is named tensorflow.py. It is being run in an environment where it isn't started as the "main" module, but is loaded by another Python program (anaconda, in this case).

When a python program is loaded this way, the interpreter reads it as a module and puts it in its list of modules (under the same name as the file), so now you have sys.modules["tensorflow"] that points to the loaded user program (and NOT to the installed tensorflow module). When the 'import tensorflow as tf' line is encountered, Python sees that "tensorflow" is already imported and simply does tf=sys.modules["tensorflow"], which is a reference to your own tensorflow.py (already a problem, but you haven't got to tf.enable_eager_execution() yet - it would fail if you did, because your tensorflow.py doesn't have such a function).

Now, the interesting part:

import tensorflow.contrib.eager as tfe

Python already has 'tensorflow' imported (your module!), so it expects to find any sub-modules in the same directory as the loaded tensorflow.py. In particular, it expects that directory to be a Python package (have init.py in it), but it obviously does not, hence the "... is not a package" error message.

Upvotes: 1

Related Questions