Python queries ImportError: cannot import name HeaderParsingError

OS: Mac OS X. When I try to run the code below, I get an error message:

ImportError: cannot import name HeaderParsingError

I have attached the trace below the code.

I tried to solve this problem in 20 minutes using Google and other stackoverflow. I tried to run:

pip install urllib3 --upprade

I also tried reinstalling the query package.

It did not help.

This seems to be a problem with my requests or urllib3 package. Has anyone had a similar problem?

Code:

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) 

This is the trace:

 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 
+7
source share
3 answers

requests comes with its own copy of the urllib3 library in the requests/packages subdirectory. This copy is somehow broken.

Reinstall requests ; either an update (you have version 2.1.0 , given the line from .packages import charade as chardet ) or reinstall the existing version.

Reinstalling with pip can be done using the --force-reinstall switch:

 pip install --force-reinstall requests==2.1.0 

or force update:

 pip install --upgrade requests 
+20
source

I had the same problem while I was just trying to make any command using pip. In the end, I found a very simple solution, just use sudo before pip .

In particular, for this problem I used the following command.

 sudo pip install --upgrade urllib3 

Hope this helps.

+3
source

This may be a problem with the urllib3 package itself . uninstalling / installing will solve the problem.

 sudo pip uninstall urllib3 sudo pip install --upgrade urllib3 

In my case, the error was:

ImportError: Unable to import the name UnrewindableBodyError

Another problem may be that urllib3 was installed via pip , and requests were installed through the yum repository, or vice versa. In this case, the fix is ​​to completely remove these libraries and install them through the same repository.

I recommend installing pip over yum to install both packages as it is easy to maintain and gives more control. Any further updates necessary to fix the OS, maintain the virtual machine, etc., will not affect the packages installed through pip.

First remove all urllib3 and query 'installations via pip and yum:

 sudo pip uninstall urllib3 -y sudo pip uninstall requests -y sudo yum remove python-urllib3 -y sudo yum remove python-requests -y 

To have one installation source, use one of the following steps, not both.

Now install both packages only through pip:

 sudo pip install --upgrade urllib3 sudo pip install --upgrade requests 

Or use only yum. I prefer pip rather than yum as described above.

To install both packages only through yum:

 sudo yum install python-urllib3 sudo yum install python-requests 

Note : always use a virtual environment to avoid conflicts when updating yum at the OS level.

+1
source

Source: https://habr.com/ru/post/1233119/


All Articles