Files
Linux/Docs/Centos安装Python.md
offends cee91802b3
Some checks failed
continuous-integration/drone Build is failing
synchronization
2025-08-25 15:57:40 +08:00

98 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

> 本文作者:丁辉
# Centos安装Python
## Python安装
> 前提条件:
>
> - 升级 Openssl 版本, 至少 OpenSSL 1.1.1 [OpenSSL源码编译升级](https://gitee.com/offends/Docs/blob/main/Linux/%E5%AE%89%E8%A3%85OpenSSL.md)
[Python源码包下载](https://www.python.org/ftp/python/)
1. 下载源码包
```bash
wget https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz
```
2. 解压源码包
```bash
tar -zxvf Python-*.tgz && cd cd Python-*
```
3. 安装编译所需的依赖
```bash
# 软件包组通常包含用于软件开发和编译的工具如编译器、调试器、make工具等。
yum groupinstall "Development Tools" -y
yum install openssl-devel bzip2-devel libffi-devel -y
```
4. 配置编译项
```bash
./configure -q --with-openssl=/usr/local/openssl --prefix=/usr/local/python3.10.13 --with-openssl-rpath=auto --enable-optimizations
```
**参数解释**
| 参数 | 描述 |
| :---------------------------------: | :----------------------------------------------------------: |
| `--with-openssl=/usr/local/openssl` | 指定OpenSSL库的安装位置。这对于编译需要OpenSSL支持的软件如Python中的_ssl模块非常重要。这里指的是OpenSSL安装在`/usr/local/openssl`目录下。 |
| `--prefix=/usr/local/python3.10.13` | 指定安装目标目录。这意味着所有安装的文件(包括可执行文件、库文件等)将被放置在`/usr/local/python3.10.13`目录下。这有助于避免与系统自带的Python版本冲突并方便管理不同版本的Python。 |
| `--with-openssl-rpath=auto` | 设置运行时链接器搜索OpenSSL库的路径为自动。这确保了在运行时能够找到正确的OpenSSL库即使它不在标准库路径下。这对于确保程序能够在不同环境中正确运行非常重要。 |
| `--enable-optimizations` | 启用额外的优化比如Profile Guided OptimizationPGO以提高Python的性能。这会增加编译时间但生成的Python解释器将运行得更快特别是对于一些计算密集型的任务。 |
| `-q` | 这个参数表示“quiet”的意思即在执行过程中尽量减少输出信息只有重要或必要的信息会被显示出来。 |
5. 开始编译
```bash
make
```
> 同时运行
>
> ```bash
> make -j 4
> ```
6. 安装
```bash
make altinstall
```
> altinstall避免替换默认的系统Python版本
7. 创建软连接
```bash
ln -s /usr/local/python3.10.13/bin/python3.10 /usr/local/bin/
```
8. 验证
```bash
python3.10 --version
```
9. 验证 ssl 模块
```bash
python3.10
```
输入
```bash
import ssl
ssl.OPENSSL_VERSION
```
## 问题记录
- make 遇到 Could not import runpy module 需要升级 PIP 版本即可解决