pythonpython-3.xurlretrieve

NameError: name 'request' is not defined in python 3


I am trying to run the basic feature extraction code from the following site:
musicinformationretrieval.
When I try to run the following code line:

kick_filepaths, snare_filepaths = stanford_mir.download_samples(collection="drum_samples_train")

it shows the following error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-7c764e7836ee> in <module>()
----> 1 kick_filepaths, snare_filepaths = stanford_mir.download_samples(collection="drum_samples_train")

C:\Users\dell\Desktop\stanford-mir-gh-pages\stanford_mir.py in download_samples(collection, download)
     89                 for i in range(1, 11):
     90                     filename = '%s_%02d.wav' % (drum_type, i)
---> 91                     urllib.urlretrieve('http://audio.musicinformationretrieval.com/drum_samples/%s' % filename,
     92                                        filename=os.path.join(collection, filename))
     93         kick_filepaths = [os.path.join(collection, 'kick_%02d.wav' % i) for i in range(1, 11)]

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

Please help me to solve this issue.


Solution

  • It seems you should use Python 2 insteed of 3. In instruction to stanford_mir there is line:

    1. If you’re totally new, the simplest solution is to download and install Anaconda for Python 2 (2.7), not Python 3.

    Also you can read why it is not working on Python 3 here.

    UPD:

    for using Python 3 you can try add code before using the lib:

    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
    

    UPD2: urlretrieve import does not help)