gpt4 book ai didi

Python 导入请求导致 Traceback - "Partially initialized module ' requests' has no attribute 'post' "

转载 作者:行者123 更新时间:2023-12-04 11:16:15 38 4
gpt4 key购买 nike

我正在尝试通过 python 触发 http 帖子。 requests模块通过 pip3 install requests 安装- 现在它也说“满足要求”,所以它被安装了。

我使用的是 Python 版本 3.8.0 .

代码:

import requests as r
headers = {'Accept' : 'application/json', 'Content-Type' : 'application/json'}
url = 'http://localhost:8083/push/message'
jsons = {"test"}
r.post(url, json=jsons, headers=headers)

错误:
Traceback (most recent call last):
File "http.py", line 1, in <module>
import requests as r
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 11, in <module>
from .exceptions import (
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap>", line 618, in _load_backward_compatible
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 199, in load_module
mod = mod._resolve()
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 113, in _resolve
return _import_module(self.mod)
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 82, in _import_module
__import__(name)
File "C:\scripts\http.py", line 5, in <module>
r.post(url, json=jsons, headers=headers)
AttributeError: partially initialized module 'requests' has no attribute 'post' (most likely due to a circular import)

当我关闭命令行并开始一个新的,然后进入 python我可以导入它:
C:\Windows\system32>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.post
<function post at 0x000001A4F7B9D310>
>>>

当我执行脚本时会发生同样的事情(仅有时) - 有时它确实有效。 (顺便说一句,当它工作时,它也会像它应该的那样发布到服务器)

有没有人遇到过类似的问题并可能想出解决方案?顺便说一句,Python 3 是我在这台机器上安装的唯一 Python 版本 - 但在其他机器上也面临类似的问题!

最佳答案

感谢 Unixia,您的回答以某种方式帮助了我:)

但是我有一些改进,我遇到了同样的错误,这是因为我将文件命名为 requests.py这与原始请求库产生了冲突

关于Python 导入请求导致 Traceback - "Partially initialized module ' requests' has no attribute 'post' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59035409/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com