目录
一、前言
在当今的软件开发和数据科学领域,Python 已经成为一种不可或缺的编程语言。然而,许多老旧的系统,如 CentOS 7.9,默认搭载的是 Python 2.7,这与 Python 社区主流的 Python 3 存在显著差异。为了满足现代开发需求,同时保持系统的稳定性和兼容性,我们需要在 CentOS 7.9 上安装并配置 Python 3.12或更高版本(这里仅以3.12为例),使其与现有的 Python 2.7 共存,并确保能够正常使用 pip3 管理 Python 3 的包依赖。
本文将详细介绍如何在 CentOS 7.9 系统中安装 Python 3.12,并确保其与系统自带的 Python 2.7 兼容共存。我们将从安装必要的依赖包开始,逐步指导您下载、编译和安装 Python 3.12,以及配置 pip3 以管理 Python 3 的包。此外,我们还将探讨如何设置环境变量和别名,以便在命令行中方便地切换和使用不同版本的 Python,从而满足大伙在Linux系统中使用Pyhton开发和数据处理方面的多样化需求。
二、Python3.12安装
1. 安装依赖包
首先,确保你已经安装了编译 Python 所需的依赖包:
[root@node01 opt]# yum install -y gcc openssl-devel bzip2-devel libffi-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 5.1 kB 00:00:00
* base: 100.125.0.40
* epel: repo.jing.rocks
* extras: 100.125.0.40
* updates: 100.125.0.40
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Package gcc-4.8.5-44.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-26.el7_9.x86_64 already installed and latest version
Package libffi-devel-3.0.13-19.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package bzip2-devel.x86_64 0:1.0.6-13.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================================
Installing:
bzip2-devel x86_64 1.0.6-13.el7 base 218 k
Transaction Summary
=========================================================================================================================================================
Install 1 Package
Total download size: 218 k
Installed size: 382 k
Downloading packages:
bzip2-devel-1.0.6-13.el7.x86_64.rpm | 218 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : bzip2-devel-1.0.6-13.el7.x86_64 1/1
Verifying : bzip2-devel-1.0.6-13.el7.x86_64 1/1
Installed:
bzip2-devel.x86_64 0:1.0.6-13.el7
Complete!
2.下载并编译 Python 3.12
这里推荐到华为云python下载目录自行查看,因为python官网基本下不动 。这里我们先下载Python 3.12 的源代码。
[root@node01 opt]# wget https://mirrors.huaweicloud.com/python/3.12.0/Python-3.12.0.tgz
--2024-08-09 19:56:12-- https://mirrors.huaweicloud.com/python/3.12.0/Python-3.12.0.tgz
Resolving mirrors.huaweicloud.com (mirrors.huaweicloud.com)... 124.70.125.153, 124.70.125.167
Connecting to mirrors.huaweicloud.com (mirrors.huaweicloud.com)|124.70.125.153|:443... connected.
HTTP request sent, awaiting response... 200
Length: 27195214 (26M) [application/octet-stream]
Saving to: ‘Python-3.12.0.tgz’
100%[===============================================================================================================>] 27,195,214 49.3MB/s in 0.5s
2024-08-09 19:56:13 (49.3 MB/s) - ‘Python-3.12.0.tgz’ saved [27195214/27195214]
然后解压进行编译安装:
[root@node01 opt]# tar xf Python-3.12.0.tgz
[root@node01 opt]# cd Python-3.12.0
[root@node01 Python-3.12.0]# ll
total 1460
-rw------- 1 admin admin 24436 Oct 2 2023 aclocal.m4
-rwx------ 1 admin admin 49348 Oct 2 2023 config.guess
-rwx------ 1 admin admin 35276 Oct 2 2023 config.sub
-rwx------ 1 admin admin 869735 Oct 2 2023 configure
-rw------- 1 admin admin 234218 Oct 2 2023 configure.ac
drwx------ 17 admin admin 4096 Oct 2 2023 Doc
drwx------ 2 admin admin 4096 Oct 2 2023 Grammar
drwx------ 4 admin admin 4096 Oct 2 2023 Include
-rwx------ 1 admin admin 15358 Oct 2 2023 install-sh
drwx------ 37 admin admin 4096 Oct 2 2023 Lib
-rw------- 1 admin admin 13936 Oct 2 2023 LICENSE
drwx------ 8 admin admin 4096 Oct 2 2023 Mac
-rw------- 1 admin admin 107531 Oct 2 2023 Makefile.pre.in
drwx------ 3 admin admin 4096 Oct 2 2023 Misc
drwx------ 16 admin admin 4096 Oct 2 2023 Modules
drwx------ 4 admin admin 4096 Oct 2 2023 Objects
drwx------ 2 admin admin 4096 Oct 2 2023 Parser
drwx------ 5 admin admin 4096 Oct 2 2023 PC
drwx------ 2 admin admin 4096 Oct 2 2023 PCbuild
drwx------ 2 admin admin 4096 Oct 2 2023 Programs
-rw------- 1 admin admin 53286 Oct 2 2023 pyconfig.h.in
drwx------ 5 admin admin 4096 Oct 2 2023 Python
-rw------- 1 admin admin 9083 Oct 2 2023 README.rst
drwx------ 24 admin admin 4096 Oct 2 2023 Tools
然后进行安装:
#这里也可以使用--prefix=targetpath 指定安装目录
[root@node01 Python-3.12.0]# ./configure --enable-optimizations
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for Python interpreter freezing... ./_bootstrap_python
checking for python3.12... no
checking for python3.12... no
checking for python3.11... no
checking for python3.10... no
checking for python3... no
checking for python... python
checking Python for regen version...
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... -std=gnu11
checking how to run the C preprocessor... gcc -std=gnu11 -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for egrep... /usr/bin/grep -E
checking for CC compiler name... gcc
checking for stdio.h... yes
checking for stdlib.h... yes
checking for s