A common error you may encounter when using Python is modulenotfounderror: no module named ‘urllib2’.
This error occurs when the Python interpreter cannot detect the urllib2 library in your current environment.
The urllib2 module is split across several submodules in Python 3. The package to install is urllib3, not urllib2.
This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.
Table of contents
- ModuleNotFoundError: no module named ‘urllib2’
- What is UrlLib?
- How to Install urllib on Windows Operating System
- How to Install urllib on Mac Operating System using pip
- How to Install urllib on Linux Operating Systems
- Installing urllib Using Anaconda
- Testing urllib
ModuleNotFoundError: no module named ‘urllib2’
What is UrlLib?
The package urllib is a URL handling module for Python. The package contains several modules for working with URLs:
- urllib.request for opening and reading URLs
- urllib.error containing the exceptions raised by urllib.request
- urllib.parse for parsing URLs
- urllib.robotparser for passing robots.txt files
In Python 3, when importing the modules, you will find them under urllib, not urllib2.
The simplest way to install urllib is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3. The package to install is urllib3, not urllib2.
How to Install urllib on Windows Operating System
First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.
You can check your Python version with the following command:
You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.
You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.
urllib installation on Windows Using pip
To install urllib, run the following command from the command prompt.
pip3 install urllib3
How to Install urllib on Mac Operating System using pip
Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:
Download pip by running the following curl command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.
Install pip by running:
From the terminal, use pip3 to install urllib:
pip3 install urllib3
How to Install urllib on Linux Operating Systems
All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.
Installing pip for Ubuntu, Debian, and Linux Mint
sudo apt install python-pip3
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
sudo dnf install python-pip3
Installing pip for CentOS 6 and 7, and older versions of Red Hat
sudo yum install epel-release sudo yum install python-pip3
Installing pip for Arch Linux and Manjaro
sudo pacman -S python-pip
Installing pip for OpenSUSE
sudo zypper python3-pip
urllib installation on Linux with Pip
Once you have installed pip, you can install urllib using:
pip3 install urllib3
Installing urllib Using Anaconda
First, to create a conda environment to install urllib.
conda create -n urllib python=3.6
Then activate the urllib container. You will see “urllib” in parentheses next to the command line prompt.
source activate urllib
Now you’re ready to install urllib3 using conda.
Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install urllib3 using one of the following commands:
conda install -c anaconda urllib3
Check urllib Version
Once you have successfully installed urllib, you can check the version of urllib. If you used pip to install urllib, you can use pip show from your terminal.
pip show urllib3
Name: urllib3 Version: 1.26.4
AttributeError: module ‘urllib’ has no attribute ‘__version__’
You cannot get the __version__ attribute of urllib in Python 3. The urllib library in Python 3 is split into several modules. You can get the __version__ attribute of the modules under urllib, for example, request:
import urllib.request print(urllib.request.__version__)
If you used conda to install urllib3, you can check the version using the following command within your conda environment:
conda list -f urllib3
# Name Version Build Channel urllib3 1.25.11 py_0 anaconda
Once you have urllib installed, you can test the modules, for example:
from urllib.request import urlopen with urlopen("http://www.google.com") as response: html = response.read() print(html)
Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install urllib.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
For further reading on ModuleNotFoundError for a related library, BeautifulSoup, go to the article: How to Solve Python Modulenotfounderror: no module named ‘bs4’.
Have fun and happy researching!