[英]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.