AttributeError: 部分初始化的模块“tensorflow"没有属性“config"(很可能是由于循环导入) [英] AttributeError: partially initialized module 'tensorflow' has no attribute 'config' (most likely due to a circular import)

查看:39
本文介绍了AttributeError: 部分初始化的模块“tensorflow"没有属性“config"(很可能是由于循环导入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到这个错误:

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

Here are my Tensorflow versions: tensorboard-2.3.0 tensorflow-2.3.1 tensorflow-estimator-2.3.0 tensorflow-gpu-2.3.1

我使用的是 Python 3.8.0

I am on Python 3.8.0

推荐答案

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

When python executes a script, it includes the script's directory in the python path. This makes it easy for programmers to supply their own modules for the script without an explicit install. Also, python doesn't treat that top level script as a module (or more precisely, it names it __main__).

就您而言,您将脚本命名为tensorflow.py".Python 执行脚本,当它看到 import tensorflow 时,它会导入你的模块,而不是真正的 tensorflow 包.由于它还没有名为 tensorflow 的模块(原为 __main__),它再次执行该文件并看到 import tensorflow第二次,但同样,它仍然是你的 tensorflow.py.这可能会一直持续下去,但 python 检测到循环导入并发出您看到的错误.

In your case, you named your script "tensorflow.py". Python executes the script and when it sees import tensorflow, it imports your module, not the real tensorflow package. Since it doesn't have a module named tensorflow yet (the original is __main__), it executes the file again and sees that import tensorflow a second time, but again, its still your same tensorflow.py. That could go on forever, but python detect the circular import and emits the error you see.

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

Its too bad that python does this. It makes it easier to deploy modules and packages but its kind of fragile. You have to make sure that your .py file names don't clash with anything that may show up in your python installation.

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

The solution is to rename C:\Users\Anush\Desktop\tensorflow.py. And just don't name it the same as any python package you may install.

这篇关于AttributeError: 部分初始化的模块“tensorflow"没有属性“config"(很可能是由于循环导入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆