由于CentOS7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。
python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
输入Python命令,查看可以得知是Python2.7.5版本
输入which python
which python
/usr/bin/python
可以查看位置,一般是位于/usr/bin/python目录下
安装Python3方法:
1. 安装依赖包
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2. 下载Python3
然后根据自己需求下载不同版本的Python3,我下载的是Python3.7.4
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
如果速度不够快,可以直接去官网下载,利用WinSCP等软件传到服务器上指定位置
3. 编译
先建立一个空文件夹:
mkdir /usr/local/python3
解压压缩包:
tar -xvJf Python-3.7.4.tar.xz
进入该目录:
cd Python-3.7.4
编译安装:
./configure --prefix=/usr/local/python3
make && make install
创建软链接:
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
4. 测试
输入python3测试:
python3
Python 3.7.4 (default, Jul 19 2019, 19:28:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
注意
pip是默认Python2.7的,需要使用pip3,这个才是Python3.7的
比如安装requests:
pip3 install requests