Fix ImportError: No module named ‘urllib2’ in Python 3.5 – Python Tutorial

By | July 10, 2019

urllib2 is used in python 2.x, so if you use urllib2 in python 3.x, you will get this error: No module named ‘urllib2’. To fix this error, we should use python 2.x or replace urllib.request to replace it.

ImportError No module named urllib2

urllib library in python 3.x contains:

  • urllib.request for opening and reading URLs
  • urllib.error containing the exceptions raised by urllib.request
  • urllib.parse for parsing URLs
  • urllib.robotparser for parsing robots.txt files

Here is an example for using urllib.request.

import urllib.request
with urllib.request.urlopen('http://www.python.org/') as f:
    print(f.read(300))

Leave a Reply