简体   繁体   中英

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 foundhere

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM