【问题标题】:AttributeError: module 'tensorflow' has no attribute 'python'AttributeError:模块“tensorflow”没有属性“python”
【发布时间】:2018-04-28 12:51:43
【问题描述】:
>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'

如您所见,我在 cmd(win 10) 中运行此代码。 “import tensorflow.contrib”和“import tensorflow.python”都可以,但是“tensorflow.contrib”和“tensorflow.python”命令不同。一个返回目录,另一个返回 AttributeError。

有人知道为什么吗?

【问题讨论】:

  • 我认为 pip install -U keras 应该这样做
  • 其实我有最新的keras模块,不过还是谢谢大家。

标签: python attributeerror


【解决方案1】:

tensorflow.__init__() 在从 tensorflow.python 模块导入所有内容后显式删除它对它的引用。

这样做的原因是提供对顶级tensorflow 命名空间中python 包中包含的子模块的访问。 tensorflow.core 包中的一些内容也被导入并在 tensorflow 命名空间中可用。

我认为这个想法不是直接导入tensorflow.pythonpython 的那些打算由应用程序使用的类、对象和子模块只需通过import tensorflow 即可使用。

【讨论】:

  • 感谢您的回复,我认为这听起来很合理。
  • @mhawke 困扰我的是声明import tensorflow.python 不会导致AttributeError。你有没有机会解释一下?
  • @MrTsjolder:导入成功,没有AttributeError,因为包存在。为什么会失败?问题是在执行import tensorflow.python 时,首先导入tensorflowbefore tensorflow.pythontensorflow.__init__.pytensorflow.python 显式导入它想要的内容,然后删除对python 的引用,因此它不在导入代码的命名空间中。因为tensorflow.python已经导入,所以没有再次导入,所以删除的引用依然被删除。
  • 那么这会导致 tensorflow.python.keras 的导入在哪里?
【解决方案2】:

使用以下导入行为我工作:

from tensorflow.python import keras

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2020-02-04
    • 2020-02-12
    • 2019-01-10
    • 2019-10-01
    相关资源
    最近更新 更多