问题
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 import requests File "/usr/lib/python2.7/site-packages/requests/init.py", line 43, in import urllib3 File "/usr/lib/python2.7/site-packages/urllib3/init.py", line 10, in from .connectionpool import ( File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 31, in from .connection import ( File "/usr/lib/python2.7/site-packages/urllib3/connection.py", line 45, in from .util.ssl_ import ( File "/usr/lib/python2.7/site-packages/urllib3/util/init.py", line 4, in from .request import make_headers File "/usr/lib/python2.7/site-packages/urllib3/util/request.py", line 5, in 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.
回答1:
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.
回答2:
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
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/52698283/python-urllib3-error-importerror-cannot-import-name-unrewindablebodyerror