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 Python 2 shell:

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • Related: '`request`' vs '`requests`' (the latter with '`s`'): [See](https://stackoverflow.com/questions/76440240/getting-this-error-while-installing-the-request-module-of-python?noredirect=1&lq=1#comment134785674_76440240) e.g. *[Several malicious typosquatted Python libraries found on PyPI repository](https://thehackernews.com/2021/07/several-malicious-typosquatted-python.html)*. That is, 'requests' (the real library) vs. 'request' (the malicious one). It is no wonder the [typosquatting](https://en.wikipedia.org/wiki/Typosquatting) attack was successful. – Peter Mortensen Jul 02 '23 at 17:27

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
  • 8,765
  • 9
  • 49
  • 56
Selcuk
  • 57,004
  • 12
  • 102
  • 110
2
  • urllib.requests module is available in Python 3.x. In Python 2.x, it's urllib module
  • You installed the third-party library requests, but you are trying to import a standard module.

Just you import requests.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
falsetru
  • 357,413
  • 63
  • 732
  • 636
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,768
  • 15
  • 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 doesn't have this module.

If you want to use urlopen, you don't need to install requests. You must only use Python 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
qvpham
  • 1,896
  • 9
  • 17
  • Re *"The module request exists only on Python 3"*: What? – Peter Mortensen Jul 02 '23 at 17:35
  • '`request`' vs '`requests`' (the latter with '`s`'): [See](https://stackoverflow.com/questions/76440240/getting-this-error-while-installing-the-request-module-of-python?noredirect=1&lq=1#comment134785674_76440240) e.g. *[Several malicious typosquatted Python libraries found on PyPI repository](https://thehackernews.com/2021/07/several-malicious-typosquatted-python.html)*. That is, 'requests' (the real library) vs. 'request' (the malicious one). It is no wonder the [typosquatting](https://en.wikipedia.org/wiki/Typosquatting) attack was successful. – Peter Mortensen Jul 02 '23 at 17:37