2019-01-11 Ubuntu 安装 python 2.7 + pip 引入module

就学习而言,现在建议学python3版本,但有时还用的上python2.7,比如我想通过snmptrap发送信息到slack,脚本是用python2.7写的,所以还是要安装一下环境,记录备忘。

好了,python 2.7安装很简单,apt install python默认安装的就是

axing@AX:/usr/local/bin$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

然后我需要安装一些module比如boto3, request这些,需要通过pip安装。python3和2.7安装pip的命令是不同的,

axing@AX:/usr/local/bin$ ./snmphandler.py
Traceback (most recent call last):
  File "./snmphandler_general.py", line 7, in 
    import requests
ImportError: No module named requests

axing@AX:/usr/local/bin$ pip import boto3
Command 'pip' not found, but can be installed with:
sudo apt install python-pip
#提示没装pip,可以用上面的命令安装,那就照做
axing@AX:/usr/local/bin$ sudo apt install python-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
... #过程略
axing@AX:/usr/local/bin$ sudo pip install requests
Collecting requests
  Downloading https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl (57kB)
    100% |████████████████████████████████| 61kB 2.7MB/s
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests)
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/9f/e0/accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/certifi-2018.11.29-py2.py3-none-any.whl (154kB)
    100% |████████████████████████████████| 163kB 1.6MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 3.7MB/s
Requirement already satisfied: idna<2.9,>=2.5 in /usr/lib/python2.7/dist-packages (from requests)
Installing collected packages: certifi, chardet, requests
Successfully installed certifi-2018.11.29 chardet-3.0.4 requests-2.21.0

现在再跑python程序就ok了。

你可能感兴趣的:(2019-01-11 Ubuntu 安装 python 2.7 + pip 引入module)