【问题标题】:NameError: name 'request' is not defined in python 3NameError:名称“请求”未在 python 3 中定义
【发布时间】:2017-11-18 09:07:03
【问题描述】:

我正在尝试从以下站点运行基本特征提取代码:
musicinformationretrieval.
当我尝试运行以下代码行时:

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

它显示以下错误消息:

---------------------------------------------------------------------------
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'

请帮我解决这个问题。

【问题讨论】:

    标签: python python-3.x urlretrieve


    【解决方案1】:

    看来您应该使用 Python 2 而不是 3。在 instruction 到 stanford_mir 中有一行:

    1. 如果您是新手,最简单的解决方案是下载并安装 Anaconda for Python 2 (2.7),而不是 Python 3。

    您还可以阅读为什么它不适用于 Python 3 here。

    更新:

    对于使用 Python 3,您可以尝试在使用 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 导入没有帮助)

    【讨论】:

    • 那么这一行对于 Python 3 将如何改变: urllib.urlretrieve('audio.musicinformationretrieval.com/drum_samples/test/%s' % filename, filename=os.path.join(collection, filename))
    • 最好不要更改它,因为这是外部库,它应该与 Python 2 一起使用,只需使用 Python 2。另外,我已经链接到关于 Python 之间 urllib 更改的 stackoverflow 讨论版本,有一个建议可以尝试...将代码添加到ansewer。
    • 好的。我将安装 Python 2。您所做的代码更新对我不起作用。我已经试过了。感谢您的帮助。
    猜你喜欢
    • 2017-05-20
    • 1970-01-01
    • 2017-09-22
    • 2015-01-09
    • 1970-01-01
    • 2019-08-15
    • 2013-11-21
    相关资源
    最近更新 更多