无法解决ImportError:没有名为请求的模块

Rud*_*koŭ 2 python pip python-2.7 python-3.x

我读过这个问题

1)我安装了点子并执行

 pip install requests
Run Code Online (Sandbox Code Playgroud)

并得到

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...
Run Code Online (Sandbox Code Playgroud)

2)我启动了python2 shell:

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

为什么我仍在捕获此异常?我做错了什么?

Sel*_*cuk 5

您会混淆以requestsPython 3的内置名称命名的3rd party模块urllib.request。您可以使用

import requests
Run Code Online (Sandbox Code Playgroud)

两者都适用于Python 2和3。但是,您可以使用

from urllib.request import urlopen
Run Code Online (Sandbox Code Playgroud)

仅适用于Python 3。