操作系統:Mac OS X.當我試圖運行下面的代碼,我得到的錯誤:Python的請求導入錯誤:無法導入名稱HeaderParsingError
ImportError: cannot import name HeaderParsingError
我已經把它貼下面的代碼追蹤。
我試着用20秒鐘的時間解決這個問題,使用Google和其他的stackoverflow。我試過跑步:
pip install urllib3 --upgrade
我也試過重新安裝請求包。
它沒有幫助。
這似乎是我的請求或urllib3包的問題。有沒有人有類似的問題?
代碼:
import requests
import json
def printResponse(r):
print '{} {}\n'.format(json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': ')), r)
r = requests.get('http://wikitest.orcsoftware.com/rest/api/content',
params={'title': 'new page'},
auth=('seb', '****'))
printResponse(r)
parentPage = r.json()['results'][0]
pageData = {'type': 'comment', 'container': parentPage,
'body': {'storage': {'value': "<p>A new comment</p>", 'representation': 'storage'}}}
r = requests.post('http://localhost:8080/confluence/rest/api/content',
data=json.dumps(pageData),
auth=('admin', 'admin'),
headers=({'Content-Type': 'application/json'}))
printResponse(r)
這是回溯:
Traceback (most recent call last):
File "/Users/sebastian/OneDrive/orc/restAPI/createSpace.py", line 1, in <module>
import requests
File "/Library/Python/2.7/site-packages/requests/__init__.py", line 61, in <module>
from . import utils
File "/Library/Python/2.7/site-packages/requests/utils.py", line 25, in <module>
from .compat import parse_http_list as _parse_list_header
File "/Library/Python/2.7/site-packages/requests/compat.py", ine 7, in <module>
from .packages import charade as chardet
File "/Library/Python/2.7/site-packages/requests/packages/__init__.py", line 3, in <module>
from . import urllib3
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/__init__.py", line 16, in <module>
from .connectionpool import (
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 33, in <module>
from .connection import (
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connection.py", line 41, in <module>
from .util import (
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/util/__init__.py", line 4, in <module>
from .response import is_fp_closed
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/util/response.py", line 3, in <module>
from ..exceptions import HeaderParsingError
ImportError: cannot import name HeaderParsingError
請格式化您的代碼。 –
@salmanwahed,不知道你的意思。我以推薦的方式包含了代碼。標籤有問題嗎?它適用於我的IDE(pyCharm)。 – seb
代碼中有縮進問題。 –