Python

Nomodule Named Urllib3

“URLLIB3 is a free and open-source HTTP client for Python. URLLIB is a powerful HTTP client that delivers a very easy interface. In addition, it provides various useful features such as connection pooling, TLS/SSL support, HTTP and SOCKS proxies, File upload and download, and many more.

Before using the urllib3 package, you need to ensure the package is installed and available in your python environment. Otherwise, if the package is not installed, you will face the “ModuleNotFoundError: No module named urllib3” error.

This error can also occur if you use different versions of the Python interpreter and the urllib3 package is not installed in that environment.

The purpose of the post is to aid you in resolving the “No module named urlib3” error.”

Importing urllib3

Like all external Python packages, we use the import keyword followed by the name of the package we wish to import.

For example, to import urllib3, we can run the code:

import urllib3

 
If the package is installed and available in your environment, Python will import the package and give you access to its features.

If the package is missing, you might face an import error as shown:

>>> import urllib3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'urllib3'

 

Let us discuss how you can fix this error if it occurs.

Method – Use Pip to Install the URLLIB3 Package

The missing package is the most common cause of the module not found error. For example, if the package you are importing is not installed in your system, then Python will return an error.

To fix this error for the urllib3 package, you can use pip as shown in the command below:

$ sudo pip3 install urllib3

 
or

$ sudo pip install urllib3

 
Use the first command to install urllib3 on Python3 environments. For Python 2, use the second command:


Once installed, you can re-import the module.

On Windows, you may not have access to the pip or pip3 binary in your system path. However, you can use the -m parameter in Python to launch pip.

Hence, run the command below to install urllib3 on Windows:

$ python -m pip install urllib3

 
And that should do it.

If you are using Anaconda or Miniconda as the package manager, you can run the command as shown:

conda install -c conda-forge urllib3

 

Method 3 – Add Pip to Path and Re-Install the Package

If the error persists after installing the urllib3 module, you can try fixing it by adding the pip binary into your system’s path.

Launch the command prompt and navigate to python’s binary directory. You can do this by running the command:

$ where Python

 
This should return the full path where Python is located. Copy the full path and run the command below to add Python and pip to the system path.

setx /M path "%path%;C:\path\to\python\binary"

 
The command above should add the directory you copied earlier to the system’s path. Ensure to replace the command above with the path to the Python binary.

Terminating

In this article, we discovered the cause of the “No module named urllib3” error and covered various methods you can use to fix the error.

Thanks for reading && Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list