In today’s post we would like to show how to easily fix error messages when importing the urllib2 library. The solution is pretty much applicable to any Python development tool of choice: Jupyter Notebook, PyCharm, Visual Studio IDE and so forth. The tutorial was written for Windows installations, but is mostly applicable for macOS.
What are urllib & urllib2?
Urllib2 is a Python library that works alongside Python2.X versions. It serves a similar purpose that the requests module, which allows Python to easily open and parse URL addresses mainly through the ubiquitous HTTP protocol. urllib is part of the standard Python library.
What is urllib 3?
Unlike urllib2; urllib3 is a 3rd party module that contributes additional capabilities to the Python standard libraries. If you encounter import errors when invoking urllib3 or requests you can easily troubleshoot using the Python package installer (known as PIP).
Troubleshooting the modulenotfound error for urllib2
See below the error screenshot when using Jupyter Lab:
In Python 3 the original urllib2 package is replaced by urllib, which in turn includes several modules such as:
So basically, if you are programming on Python 3.X, you can easily troubleshoot the no module found error for urllib2, by using the following import statement (import the modules you need):
import urllib.request as urllib2 import urllib.error import urllib.parse
Fix the No module named urllib3 error
You typically will encounter import errors when you try to use a module that is not part of the Python standard library and is not yet installed in your Python environment (be it Jupyter, Spyder, VSCode, PyCharm etc’. Luckily it’s quite simple to troubleshoot the urllib3 import error.
If you are using a Python environment that was manually created; open your Windows Command Line or Terminal in macOS and type:
pip install urllib3
If you are using the Anaconda or Miniconda distributions; open your Anaconda Command Prompt and type:
conda install urllib3
Errors using PIP
Couple of folks pointed me to the following errors they got when trying to PIP install urlib.request using PIP . The root cause of the errors is that urllib.request is part of the Python 3.X standard package and not being delivered as an addon package through PIP.
- ERROR: Could not find a version that satisfies the requirement urllib2 (from versions: none)
- ERROR: No matching distribution found for urllib2