Python 导入错误:没有名为请求的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24652074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:57:13  来源:igfitidea点击:

ImportError: No module named request

pythonspeech-recognition

提问by Mulagala

I am trying to install python SpeechRecognitionon my machine.When i am trying to install the package as pip install SpeechRecognition. I am getting the following error.

我正在尝试SpeechRecognition在我的机器上安装 python 。当我尝试将软件包安装为pip install SpeechRecognition. 我收到以下错误。

import json, urllib.request

ImportError: No module named request

And then i referred and installed requests as pip install requestsi am i am getting Requirement already satisfied.But still i am unable to install SpeechRecognition.Please let me know what mistake i am doing.Thanks in advance

然后我提到并安装了请求,因为pip install requests我得到了Requirement already satisfied。但我仍然无法安装SpeechRecognition。请让我知道我在做什么错误。提前谢谢

采纳答案by Martijn Pieters

The SpeechRecognitionlibrary requires Python 3.3 or up:

SpeechRecognition需要 Python 3.3 或更高版本

Requirements

[...]

The first software requirement is Python 3.3 or better. This is required to use the library.

要求

[...]

第一个软件要求是 Python 3.3 或更高版本。这是使用库所必需的。

and from the Trove classifiers:

来自 Trove 分类器:

Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.3
Programming Language :: Python :: 3.4

编程语言:: Python
编程语言:: Python :: 3
编程语言:: Python :: 3.3
编程语言:: Python :: 3.4

The urllib.requestmoduleis part of the Python 3 standard library; in Python 2 you'd use urllib2here.

urllib.request模块是 Python 3 标准库的一部分;在 Python 2 中你会urllib2在这里使用。

回答by Zzmilanzz Zzmadubashazz

You can do that using Python 2.

您可以使用 Python 2 做到这一点。

  1. Remove request
  2. Make that line: from urllib2 import urlopen
  1. 消除 request
  2. 做那行: from urllib2 import urlopen

You cannot have requestin Python 2, you need to have Python 3 or above.

你不能request在 Python 2 中拥有,你需要拥有 Python 3 或更高版本。

回答by Alexx Roche

from @Zzmilanzz's answer I used

来自@Zzmilanzz的回答我用过

try: #python3
    from urllib.request import urlopen
except: #python2
    from urllib2 import urlopen