天天看点

python381怎么安装_如何编译安装Python3?

1 基础知识

1.1 Python的介绍

Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。

1.2 Python特点

1.2.1 总体特点

– 广泛流行

– 开源免费

– 面向对象

– 可移植性

– 功能强大

– 易于使用

1.2.2 语言特点

– 优雅

– 明确

– 简单

2 最佳实践

2.1 准备工作

2.1.1 安装编译环境

yum -y install gcc gcc-c++ make expat-devel

2.1.2 下载源代码

cd ~

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

2.1.3 解压软件包

cd ~

tar -xf Python-3.7.3.tgz

2.2 安装Python

2.2.1 执行预编译

cd ~/Python-3.7.3

./configure --prefix=/usr/local/python-3.7.3

如果使用编译安装openssl,需要使用如下参数进行预编译Python

cd ~/Python-3.7.3

./configure --prefix=/usr/local/python-3.7.3 \

--with-openssl=/usr/local/openssl-1.0.2/

注:Python从设计上就运行多版本并存,所以无需卸载旧版本

2.2.2 执行编译

cd ~/Python-3.7.3

make

如果遇到以下提示,

Could not build the ssl module!

Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().

LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

请安装如下依赖包,

yum install -y openssl-devel

注:解决依赖关系后,你可能需要重新执行预编译和编译,否则新的依赖包可能无法被检测到。

另外,如果是包含CentOS 6.x以下的系统,你可能需要使用编译安装解决依赖关系,请参考以下链接,

https://www.cmdschool.org/archives/5740

如果有如下错误提示,

Failed to build these modules:

_ctypes

请安装如下依赖包,

yum install -y libffi-devel

注:解决依赖关系后,你可能需要重新执行预编译和编译,否则新的依赖包可能无法被检测到。

2.2.3 编译并安装

make install

2.2.4 配置环境变量

vim /etc/profile.d/python-3.7.3.sh

加入如下配置,

export PYTHONHOME=/usr/local/python-3.7.3

export PATH=${PYTHONHOME}/bin:$PATH

修改完配置文件,你需要通过如下命令导入环境变量,

source /etc/profile.d/python-3.7.3.sh

2.2.5 测试安装

python3 -V

可见如下提示,

Python 3.7.3

参阅文档

=====================