105

I am trying to write a program that will download mp3's off of a website then join them together but whenever I try to download the files I get this error:

Traceback (most recent call last):
File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 214, in <module> main()
File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 209, in main getMp3s()
File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 134, in getMp3s
raw_mp3.add = urllib.urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3")
AttributeError: 'module' object has no attribute 'urlretrieve'

The line that is causing this problem is

raw_mp3.add = urllib.urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3")

3 Answers 3

254

As you're using Python 3, there is no urllib module anymore. It has been split into several modules.

This would be equivalent to urlretrieve:

import urllib.request
data = urllib.request.urlretrieve("http://...")

urlretrieve behaves exactly the same way as it did in Python 2.x, so it'll work just fine.

Basically:

  • urlretrieve saves the file to a temporary file and returns a tuple (filename, headers)
  • urlopen returns a Request object whose read method returns a bytestring containing the file contents
2
  • 2
    If I wanted to download the .mp3 files into a list would this still work?
    – Sike1217
    Jul 31, 2013 at 3:39
  • 3
    Ran across this error when working through google's tensorflow machine learning tutorial (I'm new to python so your answer is greatly appreciated) tensorflow.org/tutorials/mnist/beginners/index.md Nov 13, 2015 at 3:04
11

A Python 2+3 compatible solution is:

import sys

if sys.version_info[0] >= 3:
    from urllib.request import urlretrieve
else:
    # Not Python 3 - today, it is most likely to be Python 2
    # But note that this might need an update when Python 4
    # might be around one day
    from urllib import urlretrieve

# Get file from URL like this:
urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3")
3
  • @tim654321 I changed it. You are right, there is a chance that this will be the same for Python 3 and later versions. Jun 6, 2016 at 8:06
  • A comment to your comment ("Not Python 3..."): Since you are checking >= 3, the concern regarding Python4 is not a valid one.
    – Martin R.
    Sep 1, 2017 at 17:15
  • @MartinR. or rather, ..., the notes about Python 4 should be in the >= 3 block instead. May 1, 2018 at 23:29
5

Suppose you have following lines of code

MyUrl = "www.google.com" #Your url goes here
urllib.urlretrieve(MyUrl)

If you are receiving following error message

AttributeError: module 'urllib' has no attribute 'urlretrieve'

Then you should try following code to fix the issue:

import urllib.request
MyUrl = "www.google.com" #Your url goes here
urllib.request.urlretrieve(MyUrl)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.