简体   繁体   中英

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 anaconda3 on my computer. I used 'Anaconda Prompt' to install tensorflow with pip install -q --upgrade tensorflow . It worked but when I run this piece of code (from here ):

from __future__ import absolute_import, division, print_function

import os
import matplotlib.pyplot as plt

import tensorflow as tf
import tensorflow.contrib.eager as tfe

tf.enable_eager_execution()

print("TensorFlow version: {}".format(tf.VERSION))
print("Eager execution : {}".format(tf.executing_eagerly()))

I get the following error:

Traceback (most recent call last):
File "<ipython-input-11-9a561e7b074b>", line 1, in <module>
runfile('C:/Users/emile/Desktop/tensorflow.py', wdir='C:/Users/emile/Desktop')

File "C:\Users\emile\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\emile\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/emile/Desktop/tensorflow.py", line 6, in <module>
import tensorflow as tf

File "C:\Users\emile\Desktop\tensorflow.py", line 7, in <module>
import tensorflow.contrib.eager as tfe

ModuleNotFoundError: No module named 'tensorflow.contrib'; 'tensorflow' is not a package

Maybe the problem is caused by Anaconda ?

Many thanks.

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM