说明:CentOS 7最小化安装,默认Python 2.7 ,开发需要安装python3.6.1 。
1、检查python版本
[root@bogon ~]# python -V
Python 2.7.5
[root@bogon ~]#
2、安装必要的库文件
# yum -y install zlib zlib-devel gcc
3、下载python3.6.1
# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
用base镜像源下载安装openssl-devel
yum --enablerepo=base --enablerepo=updates install openssl-devel
因为新版的pip默认要使用SSL,可以通过设置修改,先安装openssl-devel,然后编译安装,只是在编译的过程中加入 --enable-optimizations(优化构建python)
4、解压安装
# xz -d Python-3.6.1.tar.xz
# tar -xvf Python-3.6.1.tar
# cd Python-3.6.1/
# ./configure --prefix=/usr/local/python3 --enable-optimizations
# make && make install
5、永久添加环境变量
# vim/etc/profile
添加 export PATH="$PATH:/usr/local/python3/bin"
添加完成后 source /etc/profile
**临时添加环境变量为 # export PATH="$PATH:/usr/local/python3/bin" 终端结束后环境变量生效
6、打开python3.6
[root@bogon ~]# python3
Python 3.6.1 (default, May 19 2017, 04:11:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
7、修改/usr/bin/python链接
# cd/usr/bin
# mv python python.bak
# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
修改后可直接用 # python打开python3