导入语句后,包中的模块不可用

格里特

剧本:

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到名称空间中。

Jdehesa

如果您查看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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章