Centos 7上安装Python3.x

Posted martin1317

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos 7上安装Python3.x相关的知识,希望对你有一定的参考价值。

Centos7默认安装的是2.7,这里选择安装使用Python3.6.3

安装Python3.6.3

1、安装python3 需要的依赖包

yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc cc

2、下载python源码

wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

3、解压和编译

mkdir /usr/local/python3 -p
tar xf Python-3.6.3.tgz
cd Python-3.6.3
./configure --prefix=/usr/local/python3
make
make install

4、备份旧版python软链接,链接新版本python

[[email protected] ~]# ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 9 Oct 20 14:08 /usr/bin/python -> python2
[[email protected] ~]# ls -l /usr/bin/python2
lrwxrwxrwx. 1 root root 9 Oct 20 14:08 /usr/bin/python2 -> python2.7
[[email protected] ~]# ls -l /usr/bin/python2.7
-rwxr-xr-x. 1 root root 7136 Nov  5  2016 /usr/bin/python2.7

 

mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

5、修改yum配置文件

vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down

将第一行指定的python版本改为python2.7(#!/usr/bin/python 改为 #!/usr/bin/python2.7)

6、检验 Python 及 pip 版本

[[email protected] ~]# python -V
Python 3.6.3
[[email protected] ~]# pip -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)

参考原文地址:http://mp.weixin.qq.com/s/9fluqSWaDwkyAUMYiXF8dw(提供了另一种共存方式)

以上是关于Centos 7上安装Python3.x的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7上安装Python3.X及其与旧版本Python2.7.5的共存问题

Centos7上安装python3.7

CentOS 7安装Python3

CentOS7下python3.x的源码安装

CentOS 安装Python3.x常见问题

1Python学习CentOS 7 Linux环境搭建