Python requests ImportError: cannot import name HeaderParsingError - python

OS: Mac OS X. When I'm trying to run the code below, I get the error:
ImportError: cannot import name HeaderParsingError
I've attached traceback below the code.
I've tried to solve this issue for 20 min now, using Google and other stackoverflow. I have tried running:
pip install urllib3 --upgrade
I've also tried reinstalling the requests package.
It did not help.
This seems to be an issue with my requests or urllib3 package. Has anyone had a similar issue?
The 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 traceback:
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

requests comes with its own copy of the urllib3 library, in the requests/packages subdirectory. It is this copy that is somehow broken.
Reinstall requests itself; either upgrade (you have at most version 2.1.0, given the from .packages import charade as chardet line), or reinstall the existing version.
Reinstalling with pip can be done with the --force-reinstall switch:
pip install --force-reinstall requests==2.1.0
or force an upgrade:
pip install --upgrade requests

I had the same issue while I was simply trying to make any command using pip. At the end I found a very simple solution, just use sudo before pip.
Specifically for above issue, I have used following command.
sudo pip install --upgrade urllib3
Hope that will help.

It could be an issue with the "urllib3" package itself. uninstall/install will fix the problem.
sudo pip uninstall urllib3
sudo pip install --upgrade urllib3
In my case, the error was:
ImportError: cannot import name UnrewindableBodyError
Another issue could be that, urllib3 was installed via pip, and requests installed via yum repo, or vice-versa. In that case, the fix is to completely remove these libraries and install it via same repo.
I recommend pip over yum to install both packages as it is easy to maintain and gives more control. Any further yum updates required for OS patching or VM maintenance activities etc., won't impact the packages installed via pip.
First remove all installations of “urllib3” and “requests” 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 single source of installations, use either of the below steps, not both.
Now install both packages only via pip:
sudo pip install --upgrade urllib3
sudo pip install --upgrade requests
Or, use only yum. I prefer pip over yum as explained above.
To install both packages only via yum:
sudo yum install python-urllib3
sudo yum install python-requests
Note: Always use virtual environment to avoid conflicts when an yum update happens at OS level.

Related

ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.8/distutils/__init__.py)

I installed pip3 using sudo apt-get install python3-pip after that when I run the following command to install django sudo pip3 install django I get this error:
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in
from pip import main
File "/usr/lib/python3/dist-packages/pip/init.py", line 14, in
from pip.utils import get_installed_distributions, get_prog
File "/usr/lib/python3/dist-packages/pip/utils/init.py", line 23, in
from pip.locations import (
File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in
from distutils import sysconfig
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.8/distutils/init.py)
How do I fix this?
I have tried recently manually installing python3.9 version in my Ubuntu from 3.6 version using apt install python3.9. Then pip3 was broken. The issue is because distutils were not build for the 3.9 version.
So in my case I ran apt install python3.9-distutils to resolve my issue.
In your case make sure to modify 3.x version in distutils command.
This helped but then I got another error: ImportError: No module named 'pip._internal'
The following fixed it:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --force-reinstall

ModuleNotFoundError: No module named 'python_jwt' (Raspberry Pi)

I can't import correct Firebase package in Raspberry PI.
My code:
from firebase import firebase
db = firebase.FirebaseApplication("https://xyz.firebaseio.com/", None)
Error:
Traceback (most recent call last):
File "datastorage.py", line 1, in <module>
from firebase import firebase
firebase/__init__.py", line 14, in <module>
import python_jwt as jwt
ModuleNotFoundError: No module named 'python_jwt'
I tried to use this commands and it didn't help:
sudo pip install requests
sudo pip install python-firebase
pip install jwt
I use Python 3.7.3 and Raspbian Buster. All works on my PC but not on RPi 3B+.
I used advice from #naive.
pip install python_jwt
After that I solved another errors in that order:
pip install gcloud
pip install sseclient
pip install pycrypto
pip install requests-toolbelt
And now I see that It works. Problem was solved.
I think you need python_jwt instead of jwt. You can do:
pip install python_jwt
If you're referring to this library:
https://pyjwt.readthedocs.io/en/latest/
Then you need to install like this:
pip install PyJWT
And afterwards this will work:
import jwt

Python urllib3 error - ImportError: cannot import name UnrewindableBodyError

I set my cronjob to call my script at particular time(ex- 2 4 5 10 * python3 mayank/exp/test.py).
When my test.py is called I'm activating the virtualenv within my test.py script as follows.
activate = "/home/myserver/schedule_py3/bin/activate_this.py"
exec(open(activate).read())
After activating the virtual environment(which has python3 in it and the packages needed to run the script), I'm trying to import requests it is showing me error as:-
File "schedule_module/Schedule/notification_task.py", line 2, in <module>
import requests
File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python2.7/site-packages/urllib3/__init__.py", line 10, in <module>
from .connectionpool import (
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 31, in <module>
from .connection import (
File "/usr/lib/python2.7/site-packages/urllib3/connection.py", line 45, in <module>
from .util.ssl_ import (
File "/usr/lib/python2.7/site-packages/urllib3/util/__init__.py", line 4, in <module>
from .request import make_headers
File "/usr/lib/python2.7/site-packages/urllib3/util/request.py", line 5, in <module>
from ..exceptions import UnrewindableBodyError
ImportError: cannot import name UnrewindableBodyError
As I can see that it is taking python2.7. Can anyone tell me where I'm wrong?
Note- I had installed all the packages using pip3 inside my virtual environment.
exceptions import UnrewindableBodyError ImportError: cannot import
name UnrewindableBodyError
The above error is likely due to "urllib3" package being broken. uninstall/install will fix the problem:
sudo pip uninstall urllib3
sudo pip install --upgrade urllib3
Another issue could be that, urllib3 was installed via pip, and requests installed via yum repo, or vice-versa. In that case, the fix is to completely remove these libraries and install it via same repo.
I recommend pip over yum to install both packages as it is easy to maintain and gives more control. Any further yum updates required for OS patching or VM maintenance activities etc., won't impact the packages installed via pip.
First remove all installations of “urllib3” and “requests” 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
Now install both packages only via pip:
sudo pip install --upgrade urllib3
sudo pip install --upgrade requests
To install both packages only via yum:
sudo yum install python-urllib3
sudo yum install python-requests
Note: Always use virtual environment to avoid conflicts when an yum update happens at OS level.
You might want to look at your method of activating the virtual environment instead.
A good example can be found here
an example of this would be:
ex- 2 4 5 10 * /home/myserver/schedule_py3/<PATH TO VIRTUALENV PYTHON> <FULL PATH TO SCRIPT>mayank/exp/test.py
Because you use system python instead of virtualenv'ed.
First use activate, then python from your env folder.
2 4 5 10 * source /home/myserver/schedule_py3/bin/activate_this.py && python something_else.py
I was getting a slightly different error:
cannot import name 'HTTPConnectionPool' from 'urllib3.connectionpool'
It was getting caused b/c I had a file named queue.py in my app.
env/lib/python3.7/site-packages/urllib3/packages/six.py from urllib3 was trying to run
import queue
but instead of importing the right queue.py, was importing my queue.py!
I renamed my queue.py to something else and this seemed to resolve the issue.
in my server, we had install all the package using root privilege, but use in other account. I had encounter this kinds of error.
the way to resolve by:
sudo chmod 755 -R /usr/lib/python2.7/site-packages/
cheers
Oliver

Ubuntu 16.04 pip seems broken

I was installing some packages, after that pip doesn't work anymore. Even if I try some command such as: sudo pip show requets I get this error.
Traceback (most recent call last):
File "/usr/local/bin/pip", line 7, in <module>
from pip import main
File "/usr/local/lib/python3.5/dist-packages/pip/__init__.py", line 28, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/local/lib/python3.5/dist-packages/pip/vcs/subversion.py", line 9, in <module>
from pip.index import Link
File "/usr/local/lib/python3.5/dist-packages/pip/index.py", line 31, in <module>
from pip.wheel import Wheel, wheel_ext
File "/usr/local/lib/python3.5/dist-packages/pip/wheel.py", line 6, in <module>
import compileall
File "/usr/lib/python3.5/compileall.py", line 20, in <module>
from concurrent.futures import ProcessPoolExecutor
File "/usr/local/lib/python3.5/dist-packages/futures-3.1.1-py3.5.egg/concurrent/futures/__init__.py", line 8, in <module>
from concurrent.futures._base import (FIRST_COMPLETED,
File "/usr/local/lib/python3.5/dist-packages/futures-3.1.1-py3.5.egg/concurrent/futures/_base.py", line 381
raise exception_type, self._exception, self._traceback
^
SyntaxError: invalid syntax
Thanks anyways!
You seem to have installed the futures package on python3.5.
That is a python2 only backport of a python3.2+ package.
You need to delete /usr/local/lib/python3.5/dist-packages/futures-3.1.1-py3.5.egg then it should work again.
You don't need that package on python3.5, it is already included in the standard library.
You should try reinstalling pip if nothing else is working for you. Execute the following:
sudo apt-get install --reinstall python2.7
sudo apt-get purge python-pip
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
sudo python get-pip.py
That should work. After doing that, run pip --version and please post the results.
Looks like your pip is broken, can you check pip version using pip --version and maybe upgrading pip to latest version might solve your problem, just try
sudo pip install --upgrade pip , now check pip version and if it still shows the old version try to recreate the simlink using ln -s /usr/local/bin/pip /usr/bin/

ImportError: cannot import name HTTPSHandler installing get-pip.py

I'm getting this error when I try to install pip2.7.
I have python 2.7 as my default already, but I'm not sure why I'm getting this.
OS: CentOS 6.5
OpenSSL has been installed already.
I followed this guide to install Python 2.7:
http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
[root#myservername arandomfolder]# python get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 17474, in <module>
main()
File "get-pip.py", line 17466, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 17406, in bootstrap
import pip
File "/tmp/tmpXq2wUP/pip.zip/pip/__init__.py", line 10, in <module>
File "/tmp/tmpXq2wUP/pip.zip/pip/util.py", line 18, in <module>
File "/tmp/tmpXq2wUP/pip.zip/pip/_vendor/distlib/version.py", line 14, in <module>
File "/tmp/tmpXq2wUP/pip.zip/pip/_vendor/distlib/compat.py", line 31, in <module>
ImportError: cannot import name HTTPSHandler
You have a Python without SSL support compiled. Unfortunately, get-pip appears to require SSL support.
If you are using a Linux distribution, make sure OpenSSL is installed, including the headers when compiling Python from source.
So looks like i was missing the openssl-devel dependency
after I had installed that, i was able to install this.
I was having this issue on a Centos 6.5 machine even though I had openssl and openssl-devel installed. I was also using the steps from the site:
http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
I removed python packages I had installed before: virtualenv, NetworkX, and pil:
pip uninstall virtualenv
pip uninstall NetworkX
pip uninstall pil
Then I compiled Python 2.7 again and no longer had this issue. After I followed the steps in the link below, I reinstalled the above packages using pip2.7:
pip2.7 install virtualenv
pip2.7 install NetworkX
pip2.7 install pil --allow-external pil --allow-unverified pil
I have meet the same problem. I solved it by install openssl-devel and reconfigure, make, make install python3:
yum install openssl-devel
then go to the python3 directory:
./configure --with-zlib --with-openssl
make
make install

Categories