CentOS系统上安装Python3.12兼容自带Python2

目录

一、前言

二、Python3.12安装

1. 安装依赖包

2.下载并编译 Python 3.12

3.补充OpenSSL

4. 验证python3安装情况

5. 安装 pip 对应版本

下载并安装 get-pip.py

验证 pip 安装

6.其他说明

三、卸载Python3

一、前言

在当今的软件开发和数据科学领域,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 string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... yes
checking for wchar.h... yes
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking whether _XOPEN_SOURCE should be defined... no
checking for c++... c++
configure:

  By default, distutils will build C++ extension modules with "c++".
  If this is not intended, then set CXX on the configure command line.
  
checking for the platform triplet based on compiler characteristics... x86_64-linux-gnu
checking for multiarch... 
checking for PEP 11 support tier... x86_64-pc-linux-gnu/gcc has tier 1 (supported)
checking for -Wl,--no-as-needed... yes
checking for the Android API level... not Android
checking for --with-emscripten-target... 
checking for --enable-wasm-dynamic-linking... missing
checking for --enable-wasm-pthreads... missing
checking for --with-suffix... 
checking for case-insensitive build directory... no
checking LIBRARY... libpython$(VERSION)$(ABIFLAGS).a
checking LINKCC... $(PURIFY) $(CC)
checking EXPORTSYMS... 
checking for GNU ld... yes
checking for --enable-shared... no
checking for --with-static-libpython... yes
checking for --enable-profiling... no
checking LDLIBRARY... checking HOSTRUNNER... 
libpython$(VERSION)$(ABIFLAGS).a
checking for ar... ar
checking for a BSD-compatible install... /usr/bin/install -c
checking for a race-free mkdir -p... /usr/bin/mkdir -p
checking for --with-pydebug... no
checking for --with-trace-refs... no
checking for --enable-pystats... no
checking for --with-assertions... no
checking for --enable-optimizations... yes
checking whether C compiler accepts -fno-semantic-interposition... no
checking PROFILE_TASK... -m test --pgo --timeout=$(TESTTIMEOUT)
checking for --with-lto... no
checking for llvm-profdata... no
checking for --enable-bolt... no
checking BOLT_INSTRUMENT_FLAGS... 
checking BOLT_APPLY_FLAGS...  -update-debug-sections -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions -icf=1 -inline-all -split-eh -reorder-functions-use-hot-size -peepholes=none -jump-tables=aggressive -inline-ap -indirect-call-promotion=all -dyno-stats -use-gnu-stack -frame-opt=hot 
checking if gcc -std=gnu11 supports -fstrict-overflow and -fno-strict-overflow... yes
checking for --with-strict-overflow... no
checking if gcc -std=gnu11 supports -Og optimization level... yes
checking if we can add -Wextra... yes
checking whether gcc -std=gnu11 -fno-strict-aliasing accepts and needs -fno-strict-aliasing... no
checking if we can disable gcc -std=gnu11 unused-parameter warning... yes
checking if we can disable gcc -std=gnu11 int-conversion warning... no
checking if we can disable gcc -std=gnu11 missing-field-initializers warning... yes
checking if we can enable gcc -std=gnu11 sign-compare warning... yes
checking if we can enable gcc -std=gnu11 unreachable-code warning... yes
checking if we can enable gcc -std=gnu11 strict-prototypes warning... yes
checking if we can make implicit function declaration an error in gcc -std=gnu11 -Werror=implicit-function-declaration... yes
checking if we can use visibility in gcc -std=gnu11 -fvisibility=hidden... yes
checking whether pthreads are available without options... no
checking whether gcc -std=gnu11 accepts -Kpthread... no
checking whether gcc -std=gnu11 accepts -Kthread... no
checking whether gcc -std=gnu11 accepts -pthread... yes
checking whether c++ also accepts flags for thread support... yes
checking for alloca.h... yes
checking for asm/types.h... yes
checking for bluetooth.h... no
checking for conio.h... no
checking for crypt.h... yes
checking for direct.h... no
checking for dlfcn.h... yes
checking for endian.h... yes
checking for errno.h... yes
checking for fcntl.h... yes
checking for grp.h... yes
checking for ieeefp.h... no
checking for io.h... no
checking for langinfo.h... yes
checking for libintl.h... yes
checking for libutil.h... no
checking for linux/auxvec.h... yes
checking for sys/auxv.h... yes
checking for linux/fs.h... yes
checking for linux/limits.h... yes
checking for linux/memfd.h... yes
checking for linux/random.h... yes
checking for linux/soundcard.h... yes
checking for linux/tipc.h... yes
checking for linux/wait.h... yes
checking for netdb.h... yes
checking for net/ethernet.h... yes
checking for netinet/in.h... yes
checking for netpacket/packet.h... yes
checking for poll.h... yes
checking for process.h... no
checking for pthread.h... yes
checking for pty.h... yes
checking for sched.h... yes
checking for setjmp.h... yes
checking for shadow.h... yes
checking for signal.h... yes
checking for spawn.h... yes
checking for stropts.h... no
checking for sys/audioio.h... no
checking for sys/bsdtty.h... no
checking for sys/devpoll.h... no
checking for sys/endian.h... no
checking for sys/epoll.h... yes
checking for sys/event.h... no
checking for sys/eventfd.h... yes
checking for sys/file.h... yes
checking for sys/ioctl.h... yes
checking for sys/kern_control.h... no
checking for sys/loadavg.h... no
checking for sys/lock.h... no
checking for sys/memfd.h... no
checking for sys/mkdev.h... no
checking for sys/mman.h... yes
checking for sys/modem.h... no
checking for sys/param.h... yes
checking for sys/poll.h... yes
checking for sys/random.h... no
checking for sys/resource.h... yes
checking for sys/select.h... yes
checking for sys/sendfile.h... yes
checking for sys/socket.h... yes
checking for sys/soundcard.h... yes
checking for sys/stat.h... (cached) yes
checking for sys/statvfs.h... yes
checking for sys/sys_domain.h... no
checking for sys/syscall.h... yes
checking for sys/sysmacros.h... yes
checking for sys/termio.h... no
checking for sys/time.h... yes
checking for sys/times.h... yes
checking for sys/types.h... (cached) yes
checking for sys/uio.h... yes
checking for sys/un.h... yes
checking for sys/utsname.h... yes
checking for sys/wait.h... yes
checking for sys/xattr.h... yes
checking for sysexits.h... yes
checking for syslog.h... yes
checking for termios.h... yes
checking for util.h... no
checking for utime.h... yes
checking for utmp.h... yes
checking for dirent.h that defines DIR... yes
checking for library containing opendir... none required
checking for sys/mkdev.h... (cached) no
checking for sys/sysmacros.h... (cached) yes
checking for bluetooth/bluetooth.h... no
checking for net/if.h... yes
checking for linux/netlink.h... yes
checking for linux/qrtr.h... no
checking for linux/vm_sockets.h... yes
checking for linux/can.h... yes
checking for linux/can/bcm.h... yes
checking for linux/can/j1939.h... no
checking for linux/can/raw.h... yes
checking for netcan/can.h... no
checking for clock_t in time.h... yes
checking for makedev... yes
checking for le64toh... yes
checking for mode_t... yes
checking for off_t... yes
checking for pid_t... yes
checking for size_t... yes
checking for uid_t in sys/types.h... yes
checking for ssize_t... yes
checking for __uint128_t... yes
checking size of int... 4
checking size of long... 8
checking alignment of long... 8
checking size of long long... 8
checking size of void *... 8
checking size of short... 2
checking size of float... 4
checking size of double... 8
checking size of fpos_t... 16
checking size of size_t... 8
checking alignment of size_t... 8
checking size of pid_t... 4
checking size of uintptr_t... 8
checking alignment of max_align_t... 16
checking for long double... yes
checking size of long double... 16
checking size of _Bool... 1
checking size of off_t... 8
checking whether to enable large file support... no
checking size of time_t... 8
checking for pthread_t... yes
checking size of pthread_t... 8
checking size of pthread_key_t... 4
checking whether pthread_key_t is compatible with int... yes
checking for --enable-framework... no
checking for --with-dsymutil... no
checking for dyld... no
checking for --with-address-sanitizer... no
checking for --with-memory-sanitizer... no
checking for --with-undefined-behavior-sanitizer... no
checking the extension of shared libraries... .so
checking LDSHARED... $(CC) -shared
checking BLDSHARED flags... $(CC) -shared
checking CCSHARED... -fPIC
checking LINKFORSHARED... -Xlinker -export-dynamic
checking CFLAGSFORSHARED... 
checking SHLIBS... $(LIBS)
checking perf trampoline... yes
checking for sendfile in -lsendfile... no
checking for dlopen in -ldl... yes
checking for shl_load in -ldld... no
checking for uuid.h... no
checking for LIBUUID... no
checking for uuid/uuid.h... no
checking for uuid/uuid.h... (cached) no
checking for library containing sem_init... -lpthread
checking for textdomain in -lintl... no
checking aligned memory access is required... no
checking for --with-hash-algorithm... default
checking for --with-tzpath... "/usr/share/zoneinfo:/usr/lib/zoneinfo:/usr/share/lib/zoneinfo:/etc/zoneinfo"
checking for t_open in -lnsl... no
checking for socket in -lsocket... no
checking for --with-libs... no
checking for --with-system-expat... no
checking for LIBFFI... yes
checking for ffi_prep_cif_var... yes
checking for ffi_prep_closure_loc... yes
checking for ffi_closure_alloc... yes
checking for --with-system-libmpdec... no
checking for --with-decimal-contextvar... yes
checking for decimal libmpdec machine... uint128
checking for LIBNSL... no
checking for library containing yp_match... -lnsl
checking for rpc/rpc.h... yes
checking for LIBSQLITE3... no
checking for sqlite3.h... no
checking for --enable-loadable-sqlite-extensions... no
checking for gdbm.h... no
checking for ndbm.h... no
checking for ndbm presence and linker args...  ()
checking for gdbm/ndbm.h... no
checking for gdbm-ndbm.h... no
checking for db.h... no
checking for --with-dbmliborder... gdbm:ndbm:bdb
checking for _dbm module CFLAGS and LIBS...  
checking if PTHREAD_SCOPE_SYSTEM is supported... yes
checking for pthread_sigmask... yes
checking for pthread_getcpuclockid... yes
checking if --enable-ipv6 is specified... yes
checking if RFC2553 API is available... yes
checking ipv6 stack type... linux-glibc
checking CAN_RAW_FD_FRAMES... yes
checking for CAN_RAW_JOIN_FILTERS... yes
checking for --with-doc-strings... yes
checking for --with-pymalloc... yes
checking for --with-freelists... yes
checking for --with-c-locale-coercion... yes
checking for --with-valgrind... no
checking for --with-dtrace... no
checking for dlopen... yes
checking DYNLOADFILE... dynload_shlib.o
checking MACHDEP_OBJS... none
checking for accept4... yes
checking for alarm... yes
checking for bind_textdomain_codeset... yes
checking for chmod... yes
checking for chown... yes
checking for clock... yes
checking for close_range... no
checking for confstr... yes
checking for copy_file_range... no
checking for ctermid... yes
checking for dup... yes
checking for dup3... yes
checking for execv... yes
checking for explicit_bzero... no
checking for explicit_memset... no
checking for faccessat... yes
checking for fchmod... yes
checking for fchmodat... yes
checking for fchown... yes
checking for fchownat... yes
checking for fdopendir... yes
checking for fdwalk... no
checking for fexecve... yes
checking for fork... yes
checking for fork1... no
checking for fpathconf... yes
checking for fstatat... yes
checking for ftime... yes
checking for ftruncate... yes
checking for futimens... yes
checking for futimes... yes
checking for futimesat... yes
checking for gai_strerror... yes
checking for getegid... yes
checking for getentropy... no
checking for geteuid... yes
checking for getgid... yes
checking for getgrgid... yes
checking for getgrgid_r... yes
checking for getgrnam_r... yes
checking for getgrouplist... yes
checking for getgroups... yes
checking for gethostname... yes
checking for getitimer... yes
checking for getloadavg... yes
checking for getlogin... yes
checking for getpeername... yes
checking for getpgid... yes
checking for getpid... yes
checking for getppid... yes
checking for getpriority... yes
checking for _getpty... no
checking for getpwent... yes
checking for getpwnam_r... yes
checking for getpwuid... yes
checking for getpwuid_r... yes
checking for getresgid... yes
checking for getresuid... yes
checking for getrusage... yes
checking for getsid... yes
checking for getspent... yes
checking for getspnam... yes
checking for getuid... yes
checking for getwd... yes
checking for if_nameindex... yes
checking for initgroups... yes
checking for kill... yes
checking for killpg... yes
checking for lchown... yes
checking for linkat... yes
checking for lockf... yes
checking for lstat... yes
checking for lutimes... yes
checking for madvise... yes
checking for mbrtowc... yes
checking for memrchr... yes
checking for mkdirat... yes
checking for mkfifo... yes
checking for mkfifoat... yes
checking for mknod... yes
checking for mknodat... yes
checking for mktime... yes
checking for mmap... yes
checking for mremap... yes
checking for nice... yes
checking for openat... yes
checking for opendir... yes
checking for pathconf... yes
checking for pause... yes
checking for pipe... yes
checking for pipe2... yes
checking for plock... no
checking for poll... yes
checking for posix_fadvise... yes
checking for posix_fallocate... yes
checking for posix_spawn... yes
checking for posix_spawnp... yes
checking for pread... yes
checking for preadv... yes
checking for preadv2... no
checking for pthread_condattr_setclock... yes
checking for pthread_init... no
checking for pthread_kill... yes
checking for pwrite... yes
checking for pwritev... yes
checking for pwritev2... no
checking for readlink... yes
checking for readlinkat... yes
checking for readv... yes
checking for realpath... yes
checking for renameat... yes
checking for rtpSpawn... no
checking for sched_get_priority_max... yes
checking for sched_rr_get_interval... yes
checking for sched_setaffinity... yes
checking for sched_setparam... yes
checking for sched_setscheduler... yes
checking for sem_clockwait... no
checking for sem_getvalue... yes
checking for sem_open... yes
checking for sem_timedwait... yes
checking for sem_unlink... yes
checking for sendfile... yes
checking for setegid... yes
checking for seteuid... yes
checking for setgid... yes
checking for sethostname... yes
checking for setitimer... yes
checking for setlocale... yes
checking for setpgid... yes
checking for setpgrp... yes
checking for setpriority... yes
checking for setregid... yes
checking for setresgid... yes
checking for setresuid... yes
checking for setreuid... yes
checking for setsid... yes
checking for setuid... yes
checking for setvbuf... yes
checking for shutdown... yes
checking for sigaction... yes
checking for sigaltstack... yes
checking for sigfillset... yes
checking for siginterrupt... yes
checking for sigpending... yes
checking for sigrelse... yes
checking for sigtimedwait... yes
checking for sigwait... yes
checking for sigwaitinfo... yes
checking for snprintf... yes
checking for splice... yes
checking for strftime... yes
checking for strlcpy... no
checking for strsignal... yes
checking for symlinkat... yes
checking for sync... yes
checking for sysconf... yes
checking for system... yes
checking for tcgetpgrp... yes
checking for tcsetpgrp... yes
checking for tempnam... yes
checking for timegm... yes
checking for times... yes
checking for tmpfile... yes
checking for tmpnam... yes
checking for tmpnam_r... yes
checking for truncate... yes
checking for ttyname... yes
checking for umask... yes
checking for uname... yes
checking for unlinkat... yes
checking for utimensat... yes
checking for utimes... yes
checking for vfork... yes
checking for wait... yes
checking for wait3... yes
checking for wait4... yes
checking for waitid... yes
checking for waitpid... yes
checking for wcscoll... yes
checking for wcsftime... yes
checking for wcsxfrm... yes
checking for wmemcmp... yes
checking for writev... yes
checking for gcc -std=gnu11 -pthread options needed to detect all undeclared functions... none needed
checking whether dirfd is declared... yes
checking for chroot... yes
checking for link... yes
checking for symlink... yes
checking for fchdir... yes
checking for fsync... yes
checking for fdatasync... yes
checking for epoll_create... yes
checking for epoll_create1... yes
checking for kqueue... no
checking for prlimit... yes
checking for _dyld_shared_cache_contains_path... no
checking for memfd_create... no
checking for eventfd... yes
checking for ctermid_r... no
checking for flock declaration... yes
checking for flock... yes
checking for getpagesize... yes
checking for broken unsetenv... no
checking for true... true
checking for inet_aton in -lc... yes
checking for chflags... no
checking for lchflags... no
checking for ZLIB... yes
checking for BZIP2... no
checking for bzlib.h... yes
checking for BZ2_bzCompress in -lbz2... yes
checking for LIBLZMA... no
checking for lzma.h... no
checking for hstrerror... yes
checking for getservbyname... yes
checking for getservbyport... yes
checking for gethostbyname... yes
checking for gethostbyaddr... yes
checking for getprotobyname... yes
checking for inet_aton... yes
checking for inet_ntoa... yes
checking for inet_pton... yes
checking for getpeername... (cached) yes
checking for getsockname... yes
checking for accept... yes
checking for bind... yes
checking for connect... yes
checking for listen... yes
checking for recvfrom... yes
checking for sendto... yes
checking for setsockopt... yes
checking for socket... yes
checking for setgroups... yes
checking for openpty... no
checking for openpty in -lutil... yes
checking for library containing login_tty... none required
checking for forkpty... yes
checking for fseek64... no
checking for fseeko... yes
checking for fstatvfs... yes
checking for ftell64... no
checking for ftello... yes
checking for statvfs... yes
checking for dup2... yes
checking for getpgrp... yes
checking for setpgrp... (cached) yes
checking for setns... yes
checking for unshare... yes
checking for LIBCRYPT... no
checking for library containing crypt_r... -lcrypt
checking for crypt or crypt_r... yes
checking for clock_gettime... yes
checking for clock_getres... yes
checking for clock_settime... yes
checking for clock_nanosleep... yes
checking for nanosleep... yes
checking for major, minor, and makedev... yes
checking for getaddrinfo... yes
checking getaddrinfo bug... no
checking for getnameinfo... yes
checking whether struct tm is in sys/time.h or time.h... time.h
checking for struct tm.tm_zone... yes
checking for struct stat.st_rdev... yes
checking for struct stat.st_blksize... yes
checking for struct stat.st_flags... no
checking for struct stat.st_gen... no
checking for struct stat.st_birthtime... no
checking for struct stat.st_blocks... yes
checking for struct passwd.pw_gecos... yes
checking for struct passwd.pw_passwd... yes
checking for siginfo_t.si_band... yes
checking for time.h that defines altzone... no
checking for addrinfo... yes
checking for sockaddr_storage... yes
checking for sockaddr_alg... yes
checking for an ANSI C-conforming const... yes
checking for working signed char... yes
checking for prototypes... yes
checking for socketpair... yes
checking if sockaddr has sa_len member... no
checking for gethostbyname_r... yes
checking gethostbyname_r with 6 args... yes
checking for __fpu_control... yes
checking for --with-libm=STRING... default LIBM="-lm"
checking for --with-libc=STRING... default LIBC=""
checking for x64 gcc inline assembler... yes
checking whether float word ordering is bigendian... no
checking whether we can use gcc inline assembler to get and set x87 control word... yes
checking whether we can use gcc inline assembler to get and set mc68881 fpcr... no
checking for x87-style double rounding... no
checking for acosh... yes
checking for asinh... yes
checking for atanh... yes
checking for erf... yes
checking for erfc... yes
checking for expm1... yes
checking for log1p... yes
checking for log2... yes
checking whether POSIX semaphores are enabled... yes
checking for broken sem_getvalue... no
checking whether RTLD_LAZY is declared... yes
checking whether RTLD_NOW is declared... yes
checking whether RTLD_GLOBAL is declared... yes
checking whether RTLD_LOCAL is declared... yes
checking whether RTLD_NODELETE is declared... yes
checking whether RTLD_NOLOAD is declared... yes
checking whether RTLD_DEEPBIND is declared... yes
checking whether RTLD_MEMBER is declared... no
checking digit size for Python's longs... no value specified
checking for wchar.h... (cached) yes
checking size of wchar_t... 4
checking whether wchar_t is signed... yes
checking whether wchar_t is usable... no
checking whether byte ordering is bigendian... no
checking ABIFLAGS... 
checking SOABI... cpython-312-x86_64-linux-gnu
checking LDVERSION... $(VERSION)$(ABIFLAGS)
checking for --with-platlibdir... no
checking for --with-wheel-pkg-dir... no
checking whether right shift extends the sign bit... yes
checking for getc_unlocked() and friends... yes
checking for LIBREADLINE... no
checking for readline/readline.h... no
checking how to link readline... no
checking for broken nice()... no
checking for broken poll()... no
checking for working tzset()... yes
checking for tv_nsec in struct stat... yes
checking for tv_nsec2 in struct stat... no
checking for curses.h... no
checking for ncurses.h... no
checking curses module flags... no
checking for panel.h... no
checking panel flags... no
checking for term.h... no
checking whether mvwdelch is an expression... no
checking whether WINDOW has _flags... no
checking for curses function is_pad... no
checking for curses function is_term_resized... no
checking for curses function resize_term... no
checking for curses function resizeterm... no
checking for curses function immedok... no
checking for curses function syncok... no
checking for curses function wchgat... no
checking for curses function filter... no
checking for curses function has_key... no
checking for curses function typeahead... no
checking for curses function use_env... no
configure: checking for device files
checking for /dev/ptmx... yes
checking for /dev/ptc... no
checking for socklen_t... yes
checking for broken mbstowcs... no
checking for --with-computed-gotos... no value specified
checking whether gcc -std=gnu11 -pthread supports computed gotos... yes
checking for build directories... done
checking for -O2... yes
checking for glibc _FORTIFY_SOURCE/memmove bug... no
checking for gcc ipa-pure-const bug... no
checking for stdatomic.h... no
checking for builtin __atomic_load_n and __atomic_store_n functions... yes
checking for ensurepip... upgrade
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... no
checking for library containing shm_open... -lrt
checking for shm_open... yes
checking for shm_unlink... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking for --with-openssl-rpath... 
checking whether OpenSSL provides required ssl module APIs... no
checking whether OpenSSL provides required hashlib module APIs... no
checking for --with-ssl-default-suites... python
checking for --with-builtin-hashlib-hashes... md5,sha1,sha2,sha3,blake2
checking for LIBB2... no
checking for --disable-test-modules... yes
checking for stdlib extension module _multiprocessing... yes
checking for stdlib extension module _posixshmem... yes
checking for stdlib extension module fcntl... yes
checking for stdlib extension module mmap... yes
checking for stdlib extension module _socket... yes
checking for stdlib extension module grp... yes
checking for stdlib extension module ossaudiodev... yes
checking for stdlib extension module pwd... yes
checking for stdlib extension module resource... yes
checking for stdlib extension module _scproxy... n/a
checking for stdlib extension module spwd... yes
checking for stdlib extension module syslog... yes
checking for stdlib extension module termios... yes
checking for stdlib extension module pyexpat... yes
checking for stdlib extension module _elementtree... yes
checking for stdlib extension module _md5... yes
checking for stdlib extension module _sha1... yes
checking for stdlib extension module _sha2... yes
checking for stdlib extension module _sha3... yes
checking for stdlib extension module _blake2... yes
checking for stdlib extension module _crypt... yes
checking for stdlib extension module _ctypes... yes
checking for stdlib extension module _curses... missing
checking for stdlib extension module _curses_panel... missing
checking for stdlib extension module _decimal... yes
checking for stdlib extension module _dbm... missing
checking for stdlib extension module _gdbm... missing
checking for stdlib extension module nis... yes
checking for stdlib extension module readline... missing
checking for stdlib extension module _sqlite3... disabled
checking for stdlib extension module _tkinter... missing
checking for stdlib extension module _uuid... missing
checking for stdlib extension module zlib... yes
checking for stdlib extension module _bz2... yes
checking for stdlib extension module _lzma... missing
checking for stdlib extension module _ssl... missing
checking for stdlib extension module _hashlib... missing
checking for stdlib extension module _testcapi... yes
checking for stdlib extension module _testclinic... yes
checking for stdlib extension module _testinternalcapi... yes
checking for stdlib extension module _testbuffer... yes
checking for stdlib extension module _testimportmultiple... yes
checking for stdlib extension module _testmultiphase... yes
checking for stdlib extension module xxsubtype... yes
checking for stdlib extension module _xxtestfuzz... yes
checking for stdlib extension module _ctypes_test... yes
checking for stdlib extension module xxlimited... yes
checking for stdlib extension module xxlimited_35... yes
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/Setup.bootstrap
config.status: creating Modules/Setup.stdlib
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
configure: creating Modules/Setup.local
configure: creating Makefile

注意:使用 altinstall 而不是 install,以避免覆盖系统默认的 Python 版本。

但是装着装着就会出现一个这样的东西出来:

这是由于python3.10之后版本不在支持libressl使用ssl,需要使用openssl安装来解决编译安装 python时候遇到的ssl模块导入失败的问题,这里需要用的openssl1.1.1版本或者更高版本。

3.补充OpenSSL

这里以openssl-1.1.1为例,官网下载速度太慢,这里我直接下载以前用过的离线包了。有需要的小伙伴可以在笔者资源里自取。

[root@node01 opt]# tar zxf openssl-1.1.1t.tar.gz 
[root@node01 opt]# cd openssl-1.1.1t
#指定安装的地址
[root@node01 openssl-1.1.1t]# ./config --prefix=/usr/local/openssl 
Operating system: x86_64-whatever-linux2
Configuring OpenSSL version 1.1.1t (0x1010114fL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)         ***
***                                                                ***
**********************************************************************
[root@node01 openssl-1.1.1t]#make -j && make install 
……

 然后等待它一段时间安装完成后,我们需要重新进入python3.12的安装目录,清理一下之前编译过的python信息,然后重新编译:

[root@node01 openssl-1.1.1t]# cd /opt/Python-3.12.0
[root@node01 Python-3.12.0]# make clean
find . -depth -name '__pycache__' -exec rm -rf {} ';'
find . -name '*.py[co]' -exec rm -f {} ';'
find . -name '*.[oa]' -exec rm -f {} ';'
find . -name '*.s[ol]' -exec rm -f {} ';'
find . -name '*.so.[0-9]*.[0-9]*' -exec rm -f {} ';'
find . -name '*.lto' -exec rm -f {} ';'
find . -name '*.wasm' -exec rm -f {} ';'
find . -name '*.lst' -exec rm -f {} ';'
find build -name 'fficonfig.h' -exec rm -f {} ';' || true
find build -name '*.py' -exec rm -f {} ';' || true
find build -name '*.py[co]' -exec rm -f {} ';' || true
rm -f pybuilddir.txt
rm -f Lib/lib2to3/*Grammar*.pickle
rm -f _bootstrap_python
rm -f python.html python*.js python.data python*.symbols python*.map
rm -f ./usr/local/lib/python3.12/os.py
rm -f Programs/_testembed Programs/_freeze_module
rm -f Python/deepfreeze/*.[co]
rm -f Python/frozen_modules/*.h
rm -f Python/frozen_modules/MANIFEST
find build -type f -a ! -name '*.gc??' -exec rm -f {} ';'
rm -f Include/pydtrace_probes.h
rm -f profile-gen-stamp
# Profile data.
rm -f *.fdata
# Pristine binaries before BOLT optimization.
rm -f *.prebolt
# BOLT instrumented binaries.
rm -f *.bolt_inst
make[1]: Entering directory `/opt/Python-3.12.0'
find . -name '*.gc??' -exec rm -f {} ';'
find . -name '*.profclang?' -exec rm -f {} ';'
find . -name '*.dyn' -exec rm -f {} ';'
rm -f /opt/Python-3.12.0/coverage.info
rm -rf /opt/Python-3.12.0/lcov-report
rm -f profile-run-stamp
rm -f profile-bolt-stamp
make[1]: Leaving directory `/opt/Python-3.12.0'
#带上openssl重新开始
[root@node01 Python-3.12.0]#./configure --with-openssl=/usr/local/openssl --with-openssl-rpath=auto

这次带上openssl重新安装就可以了。

最后重新make一下即可。

[root@node01 Python-3.12.0]#make -j && make install
……

4. 验证python3安装情况

验证 Python 3.10 是否安装成功:

[root@node01 admin]# /usr/local/bin/python3.12 --version
Python 3.12.0

但是这样验证版本比较麻烦,时间长了如果不记得是否安装位置,或者使用的时候很不方便,因此可以在设置一下Python3的默认版本。 

4. 设置默认 Python 版本

安装完成后,我们可以通过建立软链接的方式来设置默认的 Python 版本:

[root@node01 admin]# ln -sf /usr/local/bin/python3.12 /usr/bin/python3
[root@node01 admin]# python3 --version
Python 3.12.0
[root@node01 admin]# python2 --version
Python 2.7.5

这样在使用不同版本的python时就方便多了。 

然后我们可以看一下opensslen模块是不是正常的:

[root@node01 admin]# python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 1.1.1t  7 Feb 2023

 正常显示咱们的安装版本即可。

5. 安装 pip 对应版本

同样的,作为python的包管理工具pip也很重要,一般情况下像python3.12在安装的时候会自动安装对应的pip。如果有时候没有安装也不要紧,只需要获取其脚本后用python安装即可。安装 pip 对应版本的方法如下:

下载并安装 get-pip.py
[root@node01 admin]# wget https://bootstrap.pypa.io/get-pip.py
[root@node01 admin]# /usr/local/bin/python3.12 get-pip.py
验证 pip 安装

同样也可以以建立软链接的方式设置一下默认方式并查验:

[root@node01 admin]# whereis pip
pip: /usr/local/bin/pip3.12
[root@node01 admin]# /usr/local/bin/pip3.12 --version
pip 23.2.1 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
[root@node01 admin]# ln -sf /usr/local/bin/pip3.12 /usr/bin/pip3
[root@node01 admin]# pip3 --version
pip 23.2.1 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)

6.其他说明

需要注意的是,一般情况pip3默认使用的网络连接源都比较差,比如直接下载pandas的时候可能会出现这种问题:

[root@node01 admin]# pip3 install pandas
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f1690cb8b00>: Failed to establish a new connection: [Errno 101] Network is unreachable')': /simple/pandas/
Collecting pandas
  Obtaining dependency information for pandas from https://files.pythonhosted.org/packages/40/10/79e52ef01dfeb1c1ca47a109a01a248754ebe990e159a844ece12914de83/pandas-2.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata

所以一般建议在使用的时候最好指定国内镜像源来进行下载:

[root@node01 admin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pandas
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/40/10/79e52ef01dfeb1c1ca47a109a01a248754ebe990e159a844ece12914de83/pandas-2.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.7/12.7 MB 55.9 MB/s eta 0:00:00
Collecting numpy>=1.26.0 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2c/f3/61eeef119beb37decb58e7cb29940f19a1464b8608f2cab8a8616aba75fd/numpy-2.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.2/19.2 MB 48.9 MB/s eta 0:00:00
Collecting python-dateutil>=2.8.2 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 229.9/229.9 kB 41.3 MB/s eta 0:00:00
Collecting pytz>=2020.1 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9c/3d/a121f284241f08268b21359bd425f7d4825cffc5ac5cd0e1b3d82ffd2b10/pytz-2024.1-py2.py3-none-any.whl (505 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 505.5/505.5 kB 52.4 MB/s eta 0:00:00
Collecting tzdata>=2022.7 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/65/58/f9c9e6be752e9fcb8b6a0ee9fb87e6e7a1f6bcab2cdc73f02bb7ba91ada0/tzdata-2024.1-py2.py3-none-any.whl (345 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 345.4/345.4 kB 50.4 MB/s eta 0:00:00
Collecting six>=1.5 (from python-dateutil>=2.8.2->pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: pytz, tzdata, six, numpy, python-dateutil, pandas
Successfully installed numpy-2.0.1 pandas-2.2.2 python-dateutil-2.9.0.post0 pytz-2024.1 six-1.16.0 tzdata-2024.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

[notice] A new release of pip is available: 23.2.1 -> 24.2
[notice] To update, run: pip3 install --upgrade pip
[root@node01 admin]# pip3 list | grep pandas
pandas          2.2.2

[notice] A new release of pip is available: 23.2.1 -> 24.2
[notice] To update, run: pip3 install --upgrade pip
[root@node01 admin]# python3 -c "import pandas as pd; print(pd.__version__); df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}); print(df)"
2.2.2
   A  B
0  1  4
1  2  5
2  3  6

这样就能很快解决了! 

另外还有一点就是在 CentOS 系统中,如果同时安装了 Python 2 和 Python 3,默认情况下,yum 和其他基于 Python 的系统工具(如 rpmdnf 等)通常会使用 Python 2。这是因为这些工具的脚本头(shebang)通常会指定为 #!/usr/bin/python 或 #!/usr/bin/python2。 

所以如果咱们将系统的默认 Python 版本更改为 Python 3(sudo ln -sf /usr/bin/python3 /usr/bin/python
),那么 yum 和其他系统工具可能会因为找不到 Python 2 而无法正常工作。为了避免这种情况,我们需要确保这些工具的脚本头指向 Python 2。方式也很简单:

修改 yum 脚本头

打开 /usr/bin/yum 文件,并将其第一行(shebang)修改为指向 Python 2:

[root@node01 admin]# sed -i '1s/^/#!\/usr\/bin\/python2\n/' /usr/bin/yum

或者手动编辑 /usr/bin/yum 文件:

[root@node01 admin]# vi /usr/bin/yum

将第一行修改为:

#!/usr/bin/python2

修改其他相关脚本

类似地,如果我们需要修改其他基于 Python 的系统工具脚本,例如 /usr/libexec/urlgrabber-ext-down

[root@node01 admin]#sed -i '1s/^/#!\/usr\/bin\/python2\n/' /usr/libexec/urlgrabber-ext-down

或者手动编辑 /usr/libexec/urlgrabber-ext-down 文件:

[root@node01 admin]#vi /usr/libexec/urlgrabber-ext-down

将第一行修改为:

#!/usr/bin/python2

验证修改

修改完成后,验证 yum 是否正常工作:

[root@node01 admin]# yum --version
3.4.3
  Installed: rpm-4.11.3-48.el7_9.x86_64 at 2024-08-05 08:08
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2021-11-24 16:33
  Committed: Michal Domonkos <mdomonko@redhat.com> at 2021-11-01

  Installed: yum-3.4.3-168.el7.centos.noarch at 2024-08-05 08:09
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2020-10-01 17:03
  Committed: CentOS Sources <bugs@centos.org> at 2020-09-29

  Installed: yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch at 2020-12-23 02:28
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2020-05-12 16:27
  Committed: Michal Domonkos <mdomonko@redhat.com> at 2020-03-12

如果 yum 正常工作,说明修改成功。

三、卸载Python3

最后附上一个简单的卸载操作"三部走":

1、使用rpm卸载python3

[root@node01 admin]#rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps 

2、删除所有残余文件

[root@node01 admin]#whereis python3 |xargs rm -frv

3、查看现有安装的python,验证是否删除干净

[root@node01 admin]#whereis python3
  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值