Python 无法解决导入错误:没有名为request的模块

Python 无法解决导入错误:没有名为request的模块,python,python-3.x,python-2.7,pip,Python,Python 3.x,Python 2.7,Pip,我读过这个 1) 我安装了pip并执行了 pip install requests 得到 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) 我启动了python2 shell: >>> from urllib.reques

我读过这个

1) 我安装了pip并执行了

 pip install requests
得到

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) 我启动了python2 shell:

>>> from urllib.request import urlopen
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named request
>>从urllib.request导入urlopen
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ImportError:没有名为request的模块
为什么我仍然抓住这个例外?我做错了什么?

  • 在Python3.x中提供。在Python2.x中,它是
  • 您已安装第三方库
    请求
    ,但正在尝试导入标准模块

为什么不直接导入
请求呢?

您将名为
请求的第三方模块与Python 3的内置
urllib.request混为一谈。你可以用

import requests
from urllib.request import urlopen
使用Python2和Python3。但是,您可以使用

import requests
from urllib.request import urlopen
仅使用Python 3。

您已经安装了请求,并且希望从请求导入模块。不一样。 模块请求仅存在于python 3上。Python2没有这个模块


->如果你想使用urlopen,你不需要安装请求。U muss只使用python 3

对我来说,有效的方法是使用以下命令安装
python pip

sudo-apt-install-python-pip

然后我用这个命令更新它

pip安装--升级pip