Skip to content Skip to sidebar Skip to footer

Modulenotfounderror: No Module Named 'tensorflow.contrib'; 'tensorflow' Is Not A Package

I'm trying to get started with Tensorflow but I'm encountering an error. I searched on Google and this website but I didn't find an answer. So let me explain. I'm currently using a

Solution 1:

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

Post a Comment for "Modulenotfounderror: No Module Named 'tensorflow.contrib'; 'tensorflow' Is Not A Package"