As stated in the urllib2 documentation:

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.

So you should instead be saying

from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)

Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/").

Answer from Eli Courtwright on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ urllib2.html
HOWTO Fetch Internet Resources Using The urllib Package โ€” Python 3.14.3 documentation
Some websites [1] dislike being browsed by programs, or send different versions to different browsers [2]. By default urllib identifies itself as Python-urllib/x.y (where x and y are the major and minor version numbers of the Python release, e.g. Python-urllib/2.5), which may confuse the site, or just plain not work. The way a browser identifies itself is through the User-Agent header [3]. When you create a Request object you can pass a dictionary of headers in.
Discussions

I don't think I can import urllib2 and I have no idea why.
Python3 uses the urllib.request module, not urllib2 https://docs.python.org/3/library/urllib.request.html#module-urllib.request More on reddit.com
๐ŸŒ r/learnpython
10
3
January 22, 2015
How to use urllib2 in python3.6?
Don't use urllib unless someone has a gun to your head. Even then, consider letting them pull the trigger. Use requests. Spend all of your extra time and happiness just enjoying life. More on reddit.com
๐ŸŒ r/learnpython
7
7
January 1, 2018
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ urllib.request.html
urllib.request โ€” Extensible library for opening URLs โ€” Python 3.14.3 documentation
The legacy urllib.urlopen function from Python 2.6 and earlier has been discontinued; urllib.request.urlopen() corresponds to the old urllib2.urlopen. Proxy handling, which was done by passing a dictionary parameter to urllib.urlopen, can be obtained by using ProxyHandler objects. The default opener raises an auditing event urllib.Request with arguments fullurl, data, headers, method taken from the request object. Changed in version 3.2: cafile and capath were added.
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ programming โ€บ python
Is there a python3 equivalent of urllib2.URLError read() - Raspberry Pi Forums
In python2, I had this bit of code: ... try: req = urllib2.Request(url,headers=headers) con = urllib2.urlopen(req) response = con.read() except (urllib2.URLError,urllib2.HTTPError), e: response = e.read() If the rest API I call has no data for the request url, it returns a 400 error with a json body containing details.
๐ŸŒ
Python
docs.python.org โ€บ 2 โ€บ library โ€บ urllib2.html
20.6. urllib2 โ€” extensible library for opening URLs โ€” Python 2.7.18 documentation
The 2to3 tool will automatically adapt imports when converting your sources to Python 3. The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world โ€” basic and digest authentication, redirections, cookies and more.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ urllib.html
urllib โ€” URL handling modules โ€” Python 3.14.3 documentation
Source code: Lib/urllib/ urllib is a package that collects several modules for working with URLs: urllib.request for opening and reading URLs, urllib.error containing the exceptions raised by urlli...
Find elsewhere
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ urllib3
urllib3
JavaScript is disabled in your browser. Please enable JavaScript to proceed ยท A required part of this site couldnโ€™t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ urllib2
urllib2 โ€“ Library for opening URLs. - Python Module of the Week
July 19, 2009 - Now available for Python 3! Buy the book! ... The urllib2 module provides an updated API for using internet resources identified by URLs.
๐ŸŒ
Python Programming
pythonprogramming.net โ€บ urllib-tutorial-python-3
Python urllib tutorial for Accessing the Internet
The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-import-urllib2-in-Python
How to import urllib2 in Python - Quora
Python 2 and Python 3 handle urllib differently; "urllib2" exists only in Python 2.
๐ŸŒ
Read the Docs
stackless.readthedocs.io โ€บ en โ€บ 2.7-slp โ€บ library โ€บ urllib2.html
20.6. urllib2 โ€” extensible library for opening URLs โ€” Stackless-Python 2.7.15 documentation
The 2to3 tool will automatically adapt imports when converting your sources to Python 3. The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world โ€” basic and digest authentication, redirections, cookies and more.
๐ŸŒ
Real Python
realpython.com โ€บ urllib-request
Python's urllib.request for HTTP Requests โ€“ Real Python
January 11, 2025 - Thatโ€™s a third-party library developed while urllib2 was still around. Itโ€™s not related to the standard library because itโ€™s an independently maintained library. Interestingly, the requests library actually uses urllib3 under the hood, and so does pip! The main answer is ease of use and security. urllib.request is considered a low-level library, which exposes a lot of the detail about the workings of HTTP requests. The Python documentation for urllib.request makes no bones about recommending requests as a higher-level HTTP client interface.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-install-urllib-and-urllib2-for-Python-3-3-2
How to install urllib and urllib2 for Python 3.3.2 - Quora
So, we donโ€™t have to install separately. And urllib2 is not available for Python 3, but included with Python 2 as a standard library. You can use this following command to search available packages for โ€˜urllibโ€™ in Ubuntu.
๐ŸŒ
Jython
jython.org โ€บ jython-old-sites โ€บ docs โ€บ library โ€บ urllib2.html
20.6. urllib2 โ€” extensible library for opening URLs โ€” Jython v2.5.2 documentation
The 2to3 tool will automatically adapt imports when converting your sources to 3.0. The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world โ€” basic and digest authentication, redirections, cookies and more.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ i don't think i can import urllib2 and i have no idea why.
r/learnpython on Reddit: I don't think I can import urllib2 and I have no idea why.
January 22, 2015 -

I need to download pdfs from a website. I know how, but the only way that I can think of is by using urllib2. That wouldn't be a problem, except urllib2 doesn't seem to be able to be imported. I'm using python3.4.2

This is what happens when I run a simple code to test it.

import urllib2

print(urllib2.__file__)

and this is the error I get:

Traceback (most recent call last):
  File      "C:/Users/ttroxell/PycharmProjects/untitled2/for_testing_functions.py", line 10, in <module>
    import urllib2
  File "C:\Python34\lib\urllib2.py", line 220
    raise AttributeError, attr
                        ^
SyntaxError: invalid syntax

I've tried adding the urllib2 file to any directory that has anything to do with python in a weak attempt to solve it. I honestly can't think of what else to do.

๐ŸŒ
WebScraping.AI
webscraping.ai โ€บ faq โ€บ urllib3 โ€บ how-do-i-upgrade-from-urllib2-to-urllib3
How do I upgrade from urllib2 to urllib3? | WebScraping.AI
import urllib3 # Configure pool with custom settings http = urllib3.PoolManager( num_pools=10, # Number of connection pools maxsize=10, # Maximum connections per pool timeout=30, # Default timeout retries=3 # Number of retries ) # Request with custom timeout response = http.request( 'GET', 'https://httpbin.org/delay/2', timeout=urllib3.Timeout(connect=5, read=10) ) import urllib3 http = urllib3.PoolManager() # Add custom headers headers = { 'User-Agent': 'MyApp/1.0', 'Accept': 'application/json', 'Authorization': 'Bearer your-token-here' } response = http.request( 'GET', 'https://httpbin.org/headers', headers=headers ) import urllib2 from urllib2 import HTTPError, URLError try: response = urllib2.urlopen('https://httpbin.org/status/404') except HTTPError as e: print(f'HTTP Error: {e.code}') except URLError as e: print(f'URL Error: {e.reason}')
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to fix โ€œimport error: no module named urllib2โ€ in python?
How to Fix "Import error: No module named urllib2" in Python? - Be on the Right Side of Change
November 7, 2022 - When you are not sure which version of python is actually in use and want your script to work with Python 2 as well as Python 3, you can include a try and except block. ... try: #For Python 2 import urllib2 as <alias> except: # If an error is seen, use the format supported in Python 3 import urllib.<module> as <alias>
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to use urllib2 in python3.6?
r/learnpython on Reddit: How to use urllib2 in python3.6?
January 1, 2018 -

So I need to run the following code, on pycharm, but there is no availability to import the Request module (not even urllib2) and trying to even slightly adapt it results in various errors (pycharm doesn't have urllib2 for python2 either)

from urllib2 import Request, urlopen

values = """
  {
    "exchange_code": "GDAX",
    "exchange_market": "BTC/USD",
    "type": "history"
  }
"""

headers = {
  'Content-Type': 'application/json',
  'X-API-KEY': '',
  'X-API-SECRET': ''
}
request = Request('https://api.coinigy.com/api/v1/data', data=values, headers=headers)

response_body = urlopen(request).read()
print(response_body)

Is there any way to get this code to run on pycharm as is, or an equivalent method that would make this work?