Install urllib2 for Python 3

A short answer (TL;DR): urllib2 is a built-in library in Python 2 that has been replaced with the urllib package in Python 3. The urllib module comes pre-installed in Python 3; therefore, you don’t have to install it.

Note: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3” – https://docs.python.org/2/library/urllib2.html.

Note: The urllib available in Python 2 has also been merged into the urllib module in Python 3.

Confirming that urllib.request contains functions and attributes from urllib2

We can do that by importing urllib2 in Python 2 and urllib.request in Python 3 and checking the attributes they contain using the dir() function.

In Python 2:

Output (truncated):

[...,  're', 'request_host', 'socket', 'splitattr', 'splithost', 'splitpasswd', 'splitport', 'splittag', 'splittype', 'splituser', 'splitvalue', 'ssl', 'sys', 'time', 'toBytes', 'unquote', 'unwrap', 'url2pathname', 'urlopen', 'urlparse', 'warnings']

In Python 3:

Output (truncated):

[..., 're', 'request_host', 'socket', 'ssl', 'string', 'sys', 'tempfile', 'thishost', 'time', 'unquote', 'unquote_to_bytes', 'unwrap', 'url2pathname', 'urlcleanup', 'urljoin', 'urlopen', 'urlparse', 'urlretrieve', 'urlsplit', 'urlunparse', 'warnings']

From the outputs above, note that Python 3’s urllib.request contains functions and attributes in Python 2’s urllib2 and more.

Using urllib2 and urllib.request

Here is an example of how you can use urllib.request to send a GET request to a website in Python 2 and 3.

If you want to run the code in any Python version 2 or 3 – use the try-except statement as follows.

Conclusion

The urllib2 package is not available in Python 3. Instead, there’s a built-in urllib.request module you can use because it contains attributes from urlllib2. Simply import urllib.request package in Python 3, and you are ready to go.