我正在尝试通过python触发http帖子。 ID Value1 Value2 Value3
1 game football 4
模块是通过requests
安装的-它现在也显示“ Requirement included”,因此已安装。
我正在使用Python版本pip3 install requests
。
代码:
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
当我执行脚本时,会发生同样的事情(仅在某些情况下会发生)-有时它确实会起作用。 (顺便说一句,当它工作时,它也会像预期的那样发布到服务器)
有人遇到过类似的问题,可能会提出解决方案吗? Python 3是我在这台机器上唯一安装的Python版本,但在其他机器上也面临类似的问题!
答案 0 :(得分:8)
感谢Unixia,您的回答以某种方式对我有所帮助:)
但是我有一些改进,我遇到了同样的错误,这是因为我将文件命名为requests.py
导致与原始请求库发生冲突
答案 1 :(得分:3)
circular dependency
当两个或多个模块相互依赖时发生。这是因为每个模块都是根据另一个模块定义的。
如果您收到请求模块错误。
AttributeError: partially initialized module 'requests' has no attribute 'post' (most likely due to a circular import)
Please try to rename the file.
此错误的错误通常是由于与您尝试导入 requests
模块的文件名冲突。
我也遇到了同样的问题,我的文件名是 email.py
,我试图导入请求模块。因此,它与 email.parser
有一些冲突。所以,我更改了文件名,它起作用了。
有关循环依赖的更多信息:https://stackabuse.com/python-circular-imports/
答案 2 :(得分:1)
您的脚本可能还有其他问题,虽然我尚未检查,但出现的错误是因为您的文件名为http.py
。似乎其他地方都在使用它,并且您遇到了循环导入问题。更改文件名。