剧本:
import tensorflow.python
tensorflow.python
结果AttributeError
:
AttributeError: module 'tensorflow' has no attribute 'python'
怎么会这样 from tensorflow import python
像预期的那样工作from tensorflow.python import keras
。我以为我了解Python导入机制的基础知识,但我不了解在什么情况下import x.y
似乎成功了,但是没有添加x.y
到名称空间中。
如果您查看tensorflow/__init__.py
,您会看到它python
在末尾显式删除了该名称:
# These symbols appear because we import the python package which
# in turn imports from tensorflow.core and tensorflow.python. They
# must come from this module. So python adds these symbols for the
# resolution to succeed.
# pylint: disable=undefined-variable
del python
del core
# pylint: enable=undefined-variable
因此,tensorflow.python
无法访问,因为python
模块对象的属性tensorflow
已被删除。不过,您仍然可以从此处导入内容:
from tensorflow.python import ops # Works
如果要专门访问该tensorflow.python
模块,则不能使用该名称,但也可以将其导入另一个名称:
from tensorflow import python as tfpython
print(tfpython)
# <module 'tensorflow.python' from '...'>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句