AttributeError: module 'tensorflow' has no attribute 'python'

守給你的承諾、 提交于 2019-12-29 01:34:13

问题


>import tensorflow

>import tensorflow.contrib

>tensorflow.contrib

module 'tensorflow.contrib' from 'D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\__init__.py'

>import tensorflow.python

>tensorflow.python

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'python'

As you can see, I run this code in a cmd(win 10). Both "import tensorflow.contrib" and "import tensorflow.python" are OK, but commands "tensorflow.contrib" and "tensorflow.python" are different. One returns a directory and the other returns AttributeError.

Does anyone have a clue why?


回答1:


tensorflow.__init__() explicitly deletes its reference to the tensorflow.python module after importing everything from it.

The reason for that is to provide access to the submodules contained in the python oackage in the toplevel tensorflow namespace. A few things from the tensorflow.core package are also imported and made available in the tensorflow namespace.

I think that the idea is not to import tensorflow.python directly. Those classes, objects, and submodules of python that are intended to be used by applications are made available simply by import tensorflow.




回答2:


Worked for me by using the following import line:

from tensorflow.python import keras

Cheers!



来源:https://stackoverflow.com/questions/47304999/attributeerror-module-tensorflow-has-no-attribute-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!