【问题标题】:AttributeError: partially initialized module 'tensorflow' has no attribute 'config' (most likely due to a circular import)AttributeError:部分初始化的模块“tensorflow”没有属性“config”(很可能是由于循环导入)
【发布时间】:2021-10-25 04:06:59
【问题描述】:

我不断收到此错误:

Traceback (most recent call last)
   File "tensorflow.py", line 1, in <module>
import tensorflow as tf
   File "C:\Users\Anush\Desktop\tensorflow.py", line 2, in <module>
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
  AttributeError: partially initialized module 'tensorflow' has no attribute 'config' (most likely due to a circular import)

这是我的代码:

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

这是我的 Tensorflow 版本: tensorboard-2.3.0 tensorflow-2.3.1 tensorflow-estimator-2.3.0 tensorflow-gpu-2.3.1

我在 Python 3.8.0

【问题讨论】:

  • 您的脚本是否命名为“tensorflow.py”?当您import tensorflow 时,您正在导入自己的文件。重命名你的脚本。

标签: python tensorflow


【解决方案1】:

就我而言,我只是将我当前的脚本名称从 code.py 重命名为 testing.py。然后完美运行。

怀疑相同的 code.py 文件名在导入 tensorflow 库中导致此循环导入错误。

【讨论】:

    【解决方案2】:

    当 python 执行一个脚本时,它会将脚本所在的目录包含在 python 路径中。这使程序员可以轻松地为脚本提供自己的模块,而无需显式安装。此外,python 不会将该顶级脚本视为一个模块(或者更准确地说,它会将其命名为 __main__)。

    在您的情况下,您将脚本命名为“tensorflow.py”。 Python 执行脚本,当它看到import tensorflow 时,它会导入您的模块,而不是真正的tensorflow 包。因为它还没有一个名为tensorflow 的模块(原来是__main__),它再次执行该文件并再次看到import tensorflow,但同样,它仍然是你的tensorflow.py。这可能会永远持续下去,但 python 会检测到循环导入并发出您看到的错误。

    python 这样做太糟糕了。它使部署模块和包变得更容易,但它有点脆弱。您必须确保您的 .py 文件名不会与您的 python 安装中可能出现的任何内容发生冲突。

    解决方法是重命名C:\Users\Anush\Desktop\tensorflow.py。只是不要将它命名为与您可能安装的任何 python 包相同的名称。

    【讨论】:

      【解决方案3】:

      也许是蟒蛇提示

      conda install tensorflow
      

      【讨论】:

        猜你喜欢
        • 2021-09-24
        • 2021-07-18
        • 2021-11-13
        • 2020-06-14
        • 2021-02-14
        • 2021-08-01
        • 1970-01-01
        • 2022-10-21
        • 2022-07-13
        相关资源
        最近更新 更多