在centos 7系统中,默认已安装python 2.7,但有些应用可能需要使用python 3。本文将介绍如何在centos 7上安装python 3,同时不影响已有的python 2.7环境,实现两者共存。

编译安装Python 3
当前Python的最新版本是Python 3.7.0,若有更新版本,安装步骤大同小异。以下是如何编译安装Python 3.7.0的详细步骤,请按提示逐行输入命令:
# 安装依赖 yum -y install wget gcc gcc-c++ libffi-devel zlib-devel <h1>下载源码(官方)</h1><p>wget <a href="https://www.php.cn/link/f1f8d9e8023b645373b6363e6a6ec18c">https://www.php.cn/link/f1f8d9e8023b645373b6363e6a6ec18c</a></p><h1>如果速度较慢,可以从xiaoz软件库下载</h1><p>wget <a href="https://www.php.cn/link/3a451d508be07d684c2face70e6ecf9d">https://www.php.cn/link/3a451d508be07d684c2face70e6ecf9d</a></p><h1>解压</h1><p>tar -xvJf Python-3.7.0.tar.xz</p><h1>进入目录</h1><p>cd Python-3.7.0</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill4102" title="Shadows Python Sensei"><img src="https://img.php.cn/upload/skill/000/000/081/178990406882325.jpg" alt="Shadows Python Sensei" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/skill4102" title="Shadows Python Sensei" class="overflowclass">Shadows Python Sensei</a> <p class="overflowclass">Python 最佳实践助手——代码规范、设计模式、性能优化、测试与类型注解。适用于编写或审查 Python 代码。</p> </div> <a rel="nofollow" href="/xiazai/skill4102" title="Shadows Python Sensei" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div><h1>编译安装</h1><p>./configure --prefix=/usr/local/python3 --enable-optimizations make -j4 && make -j4 install</p><h1>设置软连接</h1><p>ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3</p>
如果一切顺利,输入命令python3 -V即可查看版本号。若需使用原有的Python 2.7,直接输入python xxx即可,两者互不干扰。

一键安装Python 3
虽然编译安装已经非常简便,但若需要在多台服务器上安装Python 3,逐行输入命令可能会浪费时间。使用xiaoz编写的一键安装脚本,可以更快捷地完成安装,直接复制并运行以下命令:
wget <a href="https://www.php.cn/link/f00c0eeb95f5a928c3814bc3b3ee962b">https://www.php.cn/link/f00c0eeb95f5a928c3814bc3b3ee962b</a> && sh python3.sh

其它说明
一键安装脚本已在CentOS 7上经过测试,理论上也支持CentOS 6。如果在安装过程中遇到错误,请根据错误信息查找是否缺少某些依赖。如果有问题,请在评论区反馈。
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!










