ModuleNotFoundError: No module named 'urllib2' in Python

avatar

Borislav Hadzhiev

Mon Apr 18 20222 min read

ModuleNotFoundError: No module named 'urllib2' in Python #

The Python "ModuleNotFoundError: No module named 'urllib2'" occurs because the urllib2 module has been split into urllib.request and urllib.response in Python 3. To solve the error, import the module as from urllib.request import urlopen.

no module named urllib2

main.py
from urllib.request import urlopen f = urlopen('http://www.python.org/') html = f.read().decode('utf-8') print(html)

Instead of importing from urllib2.request, in Python 3 we import from urllib.request.

An alternative to importing the urlopen function like in the code snippet above is to simply import urllib.request.

main.py
import urllib.request f = urllib.request.urlopen('http://www.python.org/') html = f.read().decode('utf-8') print(html)

You can refer to the urllib.request docs for examples.

If you need a universal import that works for both Python 2 and 3, use a try/except statement.

main.py
try: from urllib.request import urlopen # using Python 3 except ImportError: from urllib2 import urlopen # falls back to import from Python 2 f = urlopen('http://www.python.org/') html = f.read().decode('utf-8') print(html)

We try to import the urllib.request module (Python 3) and if we get an ImportError, we know the file is being ran in Python 2, so we import the urlopen function from urllib2.

The Python error "ModuleNotFoundError: No module named 'urllib2'" occurs for multiple reasons:

  1. Using an incorrect import statement when importing the module.
  2. Naming your module urllib2.py or urllib.py which would shadow the official module.
  3. Declaring a variable named urllib2 or urllib which would shadow the imported variable.

If you aren't sure what version of Python you're using, run the python --version command.

shell
python --version

get python version

If you are in a virtual environment, the version of Python corresponds to the version that was used to create the virtual environment.

If you have multiple versions of Python installed on your machine, your IDE might be setup to use an incorrect version.

For example, In VSCode, you can press CTRL + Shift + P or ( + Shift + P on Mac) to open the command palette.

Then type "Python select interpreter" in the field.

python select interpreter

Then select the correct python version from the drop down menu.

select correct python version

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

If you aren't sure whether your virtual environment is created using the correct Python version, try recreating it.

shell
# 👇️ deactivate deactivate # 👇️ remove the old virtual environment folder rm -rf venv # 👇️ specify correct Python version python3 -m venv venv # 👇️ activate on Unix or MacOS source venv/bin/activate # 👇️ activate on Windows (cmd.exe) venv\Scripts\activate.bat # 👇️ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # 👇️ install the modules in your requirements.txt file pip install -r requirements.txt

Your virtual environment will use the version of Python that was used to create it.

Use the search field on my Home Page to filter through my more than 1,000 articles.