The urllib module changed the way that the request
function is accessed in Python 3. This means that if you try to reference the “urlopen” function in the way that you do in Python 2, you’ll encounter the “AttributeError: ‘module’ object has no attribute ‘urlopen’” error.
This guide talks about what this error means and why it is raised. It walks through an example of this error so you can learn how to solve it.
AttributeError: ‘module’ object has no attribute ‘urlopen’
The “urllib” module provides a number of functions related to opening URLs and reading data from websites. The syntax for using this library is different between Python 2 and Python 3.
In Python 2, “urlopen” is part of the “urllib” module. This means you can import it into your code using urllib.urlopen. In Python 3, “urlopen” is part of a “request” module within the “urllib” method:
- Python 2: urllib -> urlopen
- Python 3: urllib -> request -> urlopen
The “request” module is where many of the web request functions in the “urllib” package are bundled. AttributeErrors are raised when you try to access an attribute from a module that does not contain that attribute. In this case, “urlopen” is not part of the “urllib” module.
An Example Scenario
Build a program that retrieves data from a service called JSONPlaceholder. This service provides dummy data that you can use for your example.
Retrieve a single post with the ID #2. To start, import the urllib module:
import urllib
This statement lets you use functions from within the “urllib” module in the program.
Next, make a request using “urlopen”. This will let you retrieve the data from an endpoint on the JSONPlaceholder API:
data = urllib.urlopen("https://jsonplaceholder.typicode.com/posts/2") contents = data.read() print(as_json)
This code reads data for the post with the ID #2 on the JSONPlaceholder API.
The code prints the response from our request, formatted as a JSON string, to the console using a print statement. Run the code and see what happens:
Traceback (most recent call last): File "main.py", line 4, in <module> data = urllib.urlopen("https://jsonplaceholder.typicode.com/posts/2") AttributeError: module 'urllib' has no attribute 'urlopen'
The program returns an error.
The Solution
You’re using the Python 2 syntax to access the “urlopen” method. “urlopen” is not an attribute of “urllib” in Python 3. You reference “urlopen” as an attribute of “urllib” so the program fails to run properly.
To solve this problem, import the “request” module from the “urllib” module. This module contains the “urlopen” method:
import urllib.request
Next, change the code so that you reference the urllib.request module when you retrieve data from the JSONPlaceholder API:
data = urllib.request.urlopen("https://jsonplaceholder.typicode.com/posts/2") contents = data.read().decode('utf-8') print(contents)
This code will read the contents of the API endpoint that we call. Let’s run our code:
{ "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" }
You have decoded the response you receive using the decode()
method. Do this so you can read the values returned by the urlopen()
method as a string.
Conclusion
The “urlopen” function is not an object of “urllib” in Python 3. To access this function, import “urllib.request” into your code and reference the urllib.request.urlopen()
function.
Now you have the knowledge you need to fix this Python error like a professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.