Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to download a file by URL with urllib.retrieve: 'module' object has no attribute 'retrieve'

This is my first ever Python script, so I assume I'm doing something wrong. But I can't find a clue in any of the tutorials or examples. The following code (so to speak):

import urllib

urllib.retrieve("http://zlib.net/zlib-1.2.8.tar.gz")

throws an error

AttributeError: 'module' object has no attribute 'retrieve'

How do I fix it? This is Python 3.3.

like image 216
Violet Giraffe Avatar asked Jul 15 '15 14:07

Violet Giraffe


People also ask

What is Urllib module in Python?

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 urllib.


2 Answers

[The question was solved in the comments, hence adding it as an answer now.]

The below code works. (Source : this answer)

import urllib.request
# Download the file from `url` and save it locally under `file_name`:
urllib.request.urlretrieve(url, file_name)

Note the import statement.

You need to do import urllib.request instead of import urllib.

like image 52
Sudipta Avatar answered Oct 12 '22 18:10

Sudipta


As the error says, urllib does not have a retrieve function.

In Python 2, the module did have a urlretrieve function. In Python 3, that function has been moved to urllib.request.urlretrieve.

You can find all this in the documentation: https://docs.python.org/3/library/urllib.html

like image 41
Daniel Roseman Avatar answered Oct 12 '22 17:10

Daniel Roseman