No module named urllib3
Question:
I wrote a script to call an API and ran it successfully last week. This week, it won’t run. I get back the following error message:
Traceback (most recent call last):
File "user_audit.py", line 2, in <module>
import requests
File "c:Python27libsite-packagesrequests__init__.py", line 60, in <module>
from .packages.urllib3.exceptions import DependencyWarning
File "c:Python27libsite-packagesrequestspackages__init__.py", line 29, in <module>
import urllib3
ImportError: No module named urllib3
I’ve confirmed that packages is up to date, tried uninstalling and reinstalling it, but nothing has worked so far. Can someone help?
ADDENDUM
I installed urllib3 as suggested by @MSHossain, but then got another error message. The new message referenced another file that I’d written, which had created a Python compiled file. The other file was using smptlib to attempt to send an email. I don’t understand how this would happen, but I deleted the other file and my script ran without any problems. I’ve accepted the answer below as I was able to pip install urllib3, but it should have already been included in the requests module.
Answers:
Either urllib3 is not imported or not installed.
To import, use
import urllib3
at the top of the file. To install write:
pip install urllib3
into terminal.
It could be that you did not activate the environment variable correctly.
To activate the environment variable, write
source env/bin/activate
into terminal. Here env
is the environment variable name.
set you environment by writing source env/bin/activate if env not found write virtualenv env first then source env/bin/activate , then check pip freeze if urllib3 not found there then reinstall urllib3, hope it helps.
Few minutes back, I faced the same issue. And this was because, I used virtual environment. I believe that due to venv directory, the pip installed might have stopped working.
Fortunately, I have setup downloaded in my directory. I ran the setup and chose the option to repair, and now, everything works fine.
For me in PyCharm I had to put import urllib3
at the top of the file as mentioned earlier then PyCharm gave the option to import. Even after installing it with pip
pip install urllib3
The reason it broke is that I had installed an incompatible version of urllib3
as a transient dependency of awscli
. You’ll see such conflicts when you rerun the install.
I solved it by running
pip install --upgrade requests
I already had it installed. Solved it by running pip install --upgrade urllib3
Hope it helps someone 🙂
Reinstalling urllib3 solves my problem. Run:
pip uninstall urllib3
pip install urllib3
For the sake of completness. It means the python installation you are using does not have the package installed, be sure you are using the same python installation as the one where you are installing the package.
For me what was happening is that I had a virtual environment made with pyenv, even when the virtual environment had the package installed and in its latest version, it was not found because somehow the underlaying python install was used and not the one where I had the urllib3 installed.
Solution: Use the absolute path to the python binary:
/home/[username]/.pyenv/versions/[envname]/bin/python python-script.py
I wrote a script to call an API and ran it successfully last week. This week, it won’t run. I get back the following error message:
Traceback (most recent call last):
File "user_audit.py", line 2, in <module>
import requests
File "c:Python27libsite-packagesrequests__init__.py", line 60, in <module>
from .packages.urllib3.exceptions import DependencyWarning
File "c:Python27libsite-packagesrequestspackages__init__.py", line 29, in <module>
import urllib3
ImportError: No module named urllib3
I’ve confirmed that packages is up to date, tried uninstalling and reinstalling it, but nothing has worked so far. Can someone help?
ADDENDUM
I installed urllib3 as suggested by @MSHossain, but then got another error message. The new message referenced another file that I’d written, which had created a Python compiled file. The other file was using smptlib to attempt to send an email. I don’t understand how this would happen, but I deleted the other file and my script ran without any problems. I’ve accepted the answer below as I was able to pip install urllib3, but it should have already been included in the requests module.
Either urllib3 is not imported or not installed.
To import, use
import urllib3
at the top of the file. To install write:
pip install urllib3
into terminal.
It could be that you did not activate the environment variable correctly.
To activate the environment variable, write
source env/bin/activate
into terminal. Here env
is the environment variable name.
set you environment by writing source env/bin/activate if env not found write virtualenv env first then source env/bin/activate , then check pip freeze if urllib3 not found there then reinstall urllib3, hope it helps.
Few minutes back, I faced the same issue. And this was because, I used virtual environment. I believe that due to venv directory, the pip installed might have stopped working.
Fortunately, I have setup downloaded in my directory. I ran the setup and chose the option to repair, and now, everything works fine.
For me in PyCharm I had to put import urllib3
at the top of the file as mentioned earlier then PyCharm gave the option to import. Even after installing it with pip
pip install urllib3
The reason it broke is that I had installed an incompatible version of urllib3
as a transient dependency of awscli
. You’ll see such conflicts when you rerun the install.
I solved it by running
pip install --upgrade requests
I already had it installed. Solved it by running pip install --upgrade urllib3
Hope it helps someone 🙂
Reinstalling urllib3 solves my problem. Run:
pip uninstall urllib3
pip install urllib3
For the sake of completness. It means the python installation you are using does not have the package installed, be sure you are using the same python installation as the one where you are installing the package.
For me what was happening is that I had a virtual environment made with pyenv, even when the virtual environment had the package installed and in its latest version, it was not found because somehow the underlaying python install was used and not the one where I had the urllib3 installed.
Solution: Use the absolute path to the python binary:
/home/[username]/.pyenv/versions/[envname]/bin/python python-script.py