Python urllib3 error - ImportError: cannot import name UnrewindableBodyError

Mayank Diwedi picture Mayank Diwedi · Oct 8, 2018 · Viewed 12.9k times · Source

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.

Answer

Lalit Kumar B picture Lalit Kumar B · Aug 22, 2019

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.