7

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.

melpomene
  • 81,915
  • 7
  • 76
  • 137
Violet Giraffe
  • 31,078
  • 43
  • 182
  • 317

2 Answers2

14

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

Community
  • 1
  • 1
Sudipta
  • 4,525
  • 2
  • 24
  • 42
4

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

Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842