Centos7安装Python3的方法
由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。
[root@slave02 ~]# 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.
>>> quit()
[root@slave02 ~]#
输入Python命令,查看可以得知是Python2.7.5版本
输入命令可以查看位置,一般是位于/usr/bin/python目录下。
[root@slave02 ~]# which python
/usr/bin/python
下面介绍安装Python3的方法
首先安装依赖包
[root@slave02 ~]# yum -y groupinstall "Development tools"
[root@slave02 ~]# 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
然后根据自己需求下载不同版本的Python3,我下载的是Python3.6.4
[root@slave02 opt] wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
如果速度不够快,可以直接去官网下载,利用WinSCP、FTP工具等软件传到服务器上指定位置,我下载文件存放的目录是/opt
然后解压压缩包,进入该目录,安装Python3
如果是tar.xz文件使用 tar -xJf
如果是tgz文件使用 tar -zxf
[root@slave02 opt]# tar -zxf Python-3.6.4.tgz
[root@slave02 opt]# cd Python-3.6.4
切换到Module目录下,去掉关于SSL模块的注释
[root@slave02 Python-3.6.4] cd Modules
[root@slave02 Python-3.6.4] vim Setup.dist
找到下面这段代码
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
# SSL=/usr/local/ssl
# _ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto
修改为下面所示
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
# SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
然后编译安装
[root@slave02 Python-3.6.4]# ./configure --prefix=/usr/local/python3
[root@slave02 Python-3.6.4]# make && make install
最后创建软链接
[root@slave02 ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@slave02 ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
在命令行中输入python3测试
[root@slave02 ~]# python3
Python 3.6.4 (default, Mar 23 2018, 16:58:02)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()