How to Fix AttributeError: ‘module’ object has no attribute ‘urlretrieve’

'module' object has no attribute 'urlretrieve'

AttributeError: ‘module’ object has no attribute ‘urlretrieve’ error occurs when you try to use the urlretrieve method from Python’s urllib library but haven’t imported it correctly.

There are two main reasons for the error.

  1. Not importing urlretrieve from the correct module in Python 3.
  2. Using Python 2 (where the structure of urllib was different).

How to fix it?

For Python 3

You need to ensure you are importing urlretrieve from urllib.request.

from urllib.request import urlretrieve

After this import, you can use the urlretrieve function directly:

urlretrieve(url, filename)

For example:

from urllib.request import urlretrieve

data = urlretrieve("https://appdividend.com")

print(data)

Output

('/var/folders/8v/x42znv0n7r5383bnmngj4kmw0000gn/T/tmpt51zir6j', 
  <http.client.HTTPMessage object at 0x1012b4070>)

For Python 2

In Python 2, urlretrieve is part of the urllib module (not urllib.request). So, the import statement will be:

from urllib import urlretrieve

However, I recommend you migrate to Python 3 if possible, as Python 2 has reached its end of life and is no longer maintained.

That’s it!

Related posts

AttributeError: ‘module’ object has no attribute ‘SSL_ST_INIT’

AttributeError: ‘module’ object has no attribute ‘SSLContext’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.