【问题标题】:Python module has no attribute error when importing with an 'as'使用“as”导入时,Python 模块没有属性错误
【发布时间】:2019-10-09 20:09:06
【问题描述】:

我想从 Tensorflow 库中导入 Keras。所以我做了以下事情:

import tensorflow.python.keras as keras

但是这个导入会抛出错误:

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

似乎以下两个导入都可以正常工作:

import tensorflow.python.keras
from tensorflow.python import keras

对我来说,import tensorflow.python.keras as kerasfrom tensorflow.python import keras 是相同的,但看起来并非如此。这两个导入有什么区别?

【问题讨论】:

  • import tensorflow.python.keras as keras 为我工作 tensorflow==1.13.1Python 3.7.3
  • 你能发布你的 tf 版本和 Python 的吗?
  • 我正在使用Python 3.6tensorflow==1.13.1

标签: python


【解决方案1】:

你应该试试这样的:

import tensorflow as tf
import tensorflow 

from tensorflow import keras
from keras.layers import Dense
# ...

第一行帮助您使用带有tf 短名称(asname)的tensorflow 模块。

你可以查看这个thread的名字。

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 2016-05-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2019-07-25
    相关资源
    最近更新 更多