简体   繁体   English

如何修复 AttributeError:部分初始化的模块?

[英]How to fix AttributeError: partially initialized module?

I am trying to run my script but keep getting this error:我正在尝试运行我的脚本,但不断收到此错误:

File ".\checkmypass.py", line 1, in <module>
  import requests 
line 3, in <module>
  response = requests.get(url) 
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)

How can I fix it?我该如何解决?

This can happen when there's a local file with the same name as an imported module – python sees the local file and thinks it's the module.当存在与导入模块同名的本地文件时,就会发生这种情况——python 看到本地文件并认为它是模块。

In my case, I had a file I created in the same folder called requests.py .就我而言,我在同一个文件夹中创建了一个名为requests.py文件。 So my code was actually importing that file and not the actual requests module you install with pip.所以我的代码实际上是导入该文件,不是您使用 pip 安装的实际requests模块。 Then I had another issue with a file I created called logging.py .然后我创建的一个名为logging.py的文件出现了另一个问题。 I renamed both files and the issue was resolved.我重命名了这两个文件,问题解决了。

Make sure the name of the file is not the same as the module you are importing – this will make Python think there is a circular dependency.确保文件名与你导入的模块不同——这会让 Python 认为存在循环依赖。

Also check the URL and the package you are using.还要检查 URL 和您正在使用的包。 "Most likely due to a circular import" refers to a file (module) which has a dependency on something else and is trying to be imported while it's already been imported. “最有可能是由于循环导入”是指一个文件(模块)依赖于其他东西并且在它已经被导入时试图被导入。 Once it's correct, you should have something like this:一旦正确,你应该有这样的东西:

import requests

r = requests.get("http://google.com")       
print(r.status_code)

# 200

In my particular case, this resulted from the following sequence of commands when installing vaex :在我的特殊情况下,这是由安装vaex时的以下命令序列造成的

conda install pydantic[dotenv]
# This failed: "import vaex" so retried pip.
pip install pydantic[dotenv]
# On "import vaex", got error in OP.

And the fix:和修复:

conda uninstall pydantic[dotenv]
pip install pydantic[dotenv] --force-reinstall
# Now "import vaex" worked perfectly.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 AttributeError:部分初始化模块“numpy1”? - How to fix AttributeError: partially initialized module 'numpy1'? 如何修复 AttributeError:部分初始化的模块(Visual Studio 2019) - How to fix AttributeError:partially initialized module (Visual Studio 2019) Python AttributeError:部分初始化模块 - Python AttributeError: partially initialized module 如何修复“AttributeError:部分初始化的模块‘SendEmail’没有属性‘send_email’(很可能是由于循环导入)” - How to fix 'AttributeError: partially initialized module 'SendEmail' has no attribute 'send_email' (most likely due to a circular import)' AttributeError:部分初始化的模块“pandas”没有属性“read_csv”(很可能是由于循环导入)。 我如何解决它? - AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import). How do I fix it? AttributeError:部分初始化的模块(文件名不同) - AttributeError: partially initialized module (NOT SAME FILE NAME) AttributeError:部分初始化的模块“pandas”没有属性“DataFrame” - AttributeError: partially initialized module 'pandas' has no attribute 'DataFrame' Python/Json AttributeError:部分初始化的模块“json”没有属性 - Python/Json AttributeError: partially initialized module 'json' has no attribute 不断收到这个 AttributeError:部分初始化的模块 'schedule' 没有属性 'every' - Keep getting this AttributeError: partially initialized module 'schedule' has no attribute 'every' attributererror: 部分初始化的模块“turtle”没有属性“bgcolor” - attributeerror: partially initialized module 'turtle' has no attribute 'bgcolor'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM