57

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

import json, urllib.request

ImportError: No module named request

And then i referred and installed requests as pip install requests i 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

Mulagala
  • 8,231
  • 11
  • 29
  • 48

3 Answers3

35

The SpeechRecognition library requires Python 3.3 or up:

Requirements

[...]

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

and from the Trove classifiers:

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

The urllib.request module is part of the Python 3 standard library; in Python 2 you'd use urllib2 here.

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 4
    urllib2 does not hold request parameter – Tejas Tank Nov 04 '15 at 13:21
  • No, it doesn't. Did you expect it would? Python 3 moved several things into a package namespace, one of those things is the Python 2 `urllib2` module. – Martijn Pieters Nov 04 '15 at 14:45
  • Yes, why do you think there would be a `urllib2.request` attribute? There isn't supposed to be one. – Martijn Pieters Nov 05 '15 at 19:19
  • What do you mean by "in Python 2 you'd use urllib2 here." ? It sounds like urllib2.request exists in Python 2.7 .. That's why Tejas Tank asked. (of course I know in urllib.request exists in Python3) – Chan Kim Mar 19 '19 at 04:58
  • @ChanKim: I mean that you can replace `import urllib.request` with `import urllib2`, nothing more, nothing less. Not that that will help the OP, as the package they are installing doesn't support Python 2 and will have other issues. – Martijn Pieters Mar 19 '19 at 12:23
  • @MartijnPieters I found the modules that were in urllib.request are now in urllib2 in Python2. So instead of `from urllib.requests import urlopen` we can do `from urllib2 import urlopen`. I now realize the last sentence of your answer meant this. – Chan Kim Mar 20 '19 at 05:46
  • 1
    @ChanKim: Python 2 came first, so you have the chronology the wrong way around there. Note that Python 2 reaches its end of life in [just over 9 months](https://pythonclock.org/), if you are starting any new projects you really, **really** want to use Python 3. – Martijn Pieters Mar 20 '19 at 12:16
33

You can do that using Python 2.

  1. Remove request
  2. Make that line: from urllib2 import urlopen

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

Jeff B
  • 8,572
  • 17
  • 61
  • 140
  • Good solution for Python 2 settings. –  Jan 16 '17 at 07:46
  • I've tried: `proxy_handler = urlopen.ProxyHandler({ 'http': super_proxy_url, 'https': super_proxy_url, }) opener = urlopen.build_opener(proxy_handler)` and got an Error: _proxy_handler = urlopen.ProxyHandler({ AttributeError: 'function' object has no attribute 'ProxyHandler'_ What's wrong? – Igor Savinkin Aug 23 '17 at 12:36
13

from @Zzmilanzz's answer I used

try: #python3
    from urllib.request import urlopen
except: #python2
    from urllib2 import urlopen
Alexx Roche
  • 3,151
  • 1
  • 33
  • 39