CentOS 7安装Python3.X

由于CentOS 7自带的Python是2.7,博主想用Python3.X的版本,考虑到卸载自带的2.7会带来诸多系统问题,于是就再编译安装一个Python3.X的版本了,与2.7版本共存,互不影响。

# 下载Python3.X安装包
[root@localhost Downloads]# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
# 对于tar.xz文件,需要先用 xz -d xxx.tar.xz 将 xxx.tar.xz 解压成 xxx.tar 然后再用 tar xvf xxx.tar 来解压
[root@localhost Downloads]# xz -d Python-3.6.2.tar.xz
[root@localhost Downloads]# tar xvf Python-3.6.2.tar
# 解压后,在编译前需要安装Python依赖包,否则可能会出现编译错误,如"zipimport.ZipImportError"错误
[root@localhost Downloads]# yum install zlib zlib-devel -y
[root@localhost Python-3.6.2]# ./configure --prefix=/usr/local/python3.6
[root@localhost Python-3.6.2]# make
[root@localhost Python-3.6.2]# make install   # 如果这一步出现错误,再针对性解决,一般都是缺失依赖包导致的。
# 编译安装完成后,还需要将Python3.6添加至环境变量,便于全局使用
[root@localhost Python-3.6.2]# vim /etc/profile
export PATH=$PATH:/usr/local/python3.6/bin/  #新增内容,保存退出
[root@localhost Python-3.6.2]# source /etc/profile   #刷新环境变量
# 到这里,Python3.6已经安装完毕,并可正常使用。如下,系统默认Python是2.7
[root@localhost Downloads]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> eixt()
# 退出后,我们进入python3.6
[root@localhost Downloads]# python3.6
Python 3.6.2 (default, Aug 24 2017, 14:25:11) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost Downloads]#

好了,到这里,就可以愉快的使用Python3.X了,暂时未发现问题,有问题会及时更新说明。

标签:PythonCentos 发布于:2019-11-11 03:57:01