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:
1 2 |
import urllib2 dir(urllib2) |
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:
1 2 |
import urllib.request dir(urllib.request) |
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.
1 2 3 4 5 6 7 8 9 |
# In Python 3 from urllib.request import urlopen response = urlopen("http://www.example.com/").read() print(response) # In Python 2 from urllib2 import urlopen response = urlopen("http://www.example.com/").read() print(response) |
If you want to run the code in any Python version 2 or 3 – use the try-except statement as follows.
1 2 3 4 5 6 7 8 9 10 |
# To run in Python 2 or 3 - use try-except try: # Try to import urllib.request # This will work in Python 3 only. from urllib.request import urlopen except ImportError as e: # Fall back for Python 2 from urllib2 import urlopen response = urlopen("http://www.example.com/").read() print(response) |
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.