2

I have read this question

1) I installed pip and I executed

 pip install requests

and got

Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages/requests-2.9.1-py2.7.egg
Cleaning up...

2) I started my python2 shell:

>>> from urllib.request import urlopen
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named request

Why I am still catching this exception? What I am doing wrong?

Selcuk
  • 52,758
  • 11
  • 94
  • 99
Rudziankoŭ
  • 9,663
  • 16
  • 80
  • 173

4 Answers4

6

You are confusing the 3rd party module named requests with the Python 3's built-in urllib.request. You can use

import requests

both with Python 2 and 3. However, you can use

from urllib.request import urlopen

only with Python 3.

Zulu
  • 7,776
  • 9
  • 44
  • 55
Selcuk
  • 52,758
  • 11
  • 94
  • 99
2

Why dont you just import requests?

falsetru
  • 336,967
  • 57
  • 673
  • 597
0

What worked for me was to install python-pip with this command:

sudo apt install python-pip

then I update it with this command

pip install --upgrade pip

smead
  • 1,710
  • 13
  • 23
SAC
  • 1
-1

you have installed request(s) and you want to import a module from request. It's not the same. The module request exists only on python 3. Python 2 don't have this module.

-> If u want to use urlopen, you don't need to install requests. U muss only use python 3

qvpham
  • 1,857
  • 8
  • 16