python报错No module named ‘requests’

[root@node1 ~]

# python3 deploy_web.py 
Traceback (most recent call last):
File "deploy_web.py", line 2, in
import requests
ModuleNotFoundError: No module named 'requests'

原因可能是没有装requests模块,也有可能是python环境有问题,现在先试试装requests模块

[root@node1 ~]

# pip install requests
-bash: pip: 未找到命令

结果pip也没装,只好先装pip

[root@node1 ~]

# wget https://bootstrap.pypa.io/get-pip.py
--2019-05-05 23:54:57-- https://bootstrap.pypa.io/get-pip.py
正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)… 151.101.228.175, 2a04:4e42:1a::175
正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|151.101.228.175|:443… 已连接。
已发出 HTTP 请求,正在等待回应… 200 OK
长度:1709825 (1.6M) [text/x-python]
正在保存至: “get-pip.py”
100%[==========================================================>] 1,709,825    221KB/s 用时 99s    
2019-05-05 23:56:40 (16.9 KB/s) - 已保存 “get-pip.py” [1709825/1709825])

[root@node1 ~]

# python get-pip.py 
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pip
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 101] \xe7\xbd\x91\xe7\xbb\x9c\xe4\xb8\x8d\xe5\x8f\xaf\xe8\xbe\xbe',)': /packages/f9/fb/863012b13912709c13cf5cfdbfb304fa6c727659d6290438e1a88df9d848/pip-19.1-py2.py3-none-any.whl
Downloading https://files.pythonhosted.org/packages/f9/fb/863012b13912709c13cf5cfdbfb304fa6c727659d6290438e1a88df9d848/pip-19.1-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 27kB/s
Collecting wheel
Downloading https://files.pythonhosted.org/packages/96/ba/a4702cbb6a3a485239fbe9525443446203f00771af9ac000fa3ef2788201/wheel-0.33.1-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Successfully installed pip-19.1 wheel-0.33.1

安装完成,再试一下

[root@node1 ~]

# sudo pip install requests
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Requirement already satisfied: requests in /usr/lib/python2.7/site-packages (2.6.0)

提示已经安装过了,所以,这个问题,是因为Python环境问题造成的

[root@node1 ~]

# ls /usr/bin/python*
/usr/bin/python /usr/bin/python2 /usr/bin/python2.7

可以看到,确实安装了多个Python,然后requests模块不在我刚才用的引用库里,换个python试一下

此条目发表在python分类目录,贴了标签。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注