Fix Python AttributeError: module ‘urllib’ has no attribute ‘request’ for Beginners – Python Tutorial

By | October 25, 2019

Whe you are use python to start to network programming, you may find this error: AttributeError: module ‘urllib’ has no attribute ‘request’. In this tutorial, we will introduce how to fix it.

AttributeError module 'urllib' has no attribute 'request'

Why this error occur?

You may use python 3.x to python network programming, urllib library is changed in this python verison.

How to fix this AttributeError?

You should do like this:

import urllib.request

Which means you should use urllib.request, not urllib.

Code below will not occurs error.

class CustomHTTPRedirectHandler(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, hdrs, newurl):
        return None