centos7.9 安装python3.12

环境centos7.9 

下载python3.12 

https://www.python.org/downloads/

 

安装软件包

yum -y install epel-release wget make cmake gcc bzip2-devel libffi-devel zlib-devel
 yum -y groupinstall "Development Tools"

检查openssl版本和gcc版本

[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]#

升级openssl1.1.1k执行安装命令如下:

yum install -y openssl11 openssl11-devel

 

检查库文件和头文件

[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]# ls -l /usr/lib64/libssl.so.1.1*
pto.so.1lrwxrwxrwx 1 root root     16 Sep  4 14:45 /usr/lib64/libssl.so.1.1 -> libssl.so.1.1.1k
-rwxr-xr-x 1 root root 603592 Jan 24  2024 /usr/lib64/libssl.so.1.1.1k
[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]# ls -l /usr/lib64/libcrypto.so.1.1*
lrwxrwxrwx 1 root root      19 Sep  4 14:45 /usr/lib64/libcrypto.so.1.1 -> libcrypto.so.1.1.1k
-rwxr-xr-x 1 root root 3090568 Jan 24  2024 /usr/lib64/libcrypto.so.1.1.1k
[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]#

 

切换到源码目录,执行

cd /root/Python-3.12.3
make clean

./configure \
  --enable-optimizations \
  --with-openssl=/usr \
  LDFLAGS="-L/usr/lib64/openssl11" \
  CPPFLAGS="-I/usr/include/openssl11"
然后:

bash
复制代码
make -j$(nproc)
make altinstall

验证ssl

python3.12 -c "import ssl; print(ssl.OPENSSL_VERSION)"

错误信息如下:

Python runtime state: initialized
Traceback (most recent call last):
  File "/root/Python-3.12.3/Lib/site.py", line 73, in <module>
    import os
  File "/root/Python-3.12.3/Lib/os.py", line 29, in <module>
    from _collections_abc import _check_methods
SystemError: <built-in function compile> returned NULL without setting an exception
make[1]: *** [Python/frozen_modules/codecs.h] Error 1
make[1]: *** Waiting for unfinished jobs....
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/root/Python-3.12.3/Lib/site.py", line 73, in <module>
    import os
  File "/root/Python-3.12.3/Lib/os.py", line 29, in <module>
    from _collections_abc import _check_methods
SystemError: <built-in function compile> returned NULL without setting an exception
make[1]: *** [Python/frozen_modules/abc.h] Error 1
make[1]: Leaving directory `/root/Python-3.12.3'
make: *** [profile-opt] Error 2
[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]# python3.12
Python 3.12.3 (main, Sep  4 2025, 14:55:40) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.12/ssl.py", line 100, in <module>
    import _ssl             # if we can't import it, let the error propagate
    ^^^^^^^^^^^
ModuleNotFoundError: No module named '_ssl'
>>>
[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]#

解决办法升级gcc版本

yum install -y centos-release-scl
yum install -y devtoolset-11
scl enable devtoolset-11 bash

确认:

gcc --version


应该 ≥ 11.x。

重新执行

make clean
./configure --enable-optimizations \
  LDFLAGS="-L/usr/lib64/openssl11" \
  CPPFLAGS="-I/usr/include/openssl11"
make -j$(nproc)
make altinstall

执行完毕验证

/usr/bin/install -c -m 755 Modules/_ctypes_test.cpython-312-x86_64-linux-gnu.so /usr/local/lib/python3.12/lib-dynload/_ctypes_test.cpython-312-x86_64-linux-gnu.so
/usr/bin/install -c -m 755 Modules/xxlimited.cpython-312-x86_64-linux-gnu.so /usr/local/lib/python3.12/lib-dynload/xxlimited.cpython-312-x86_64-linux-gnu.so
/usr/bin/install -c -m 755 Modules/xxlimited_35.cpython-312-x86_64-linux-gnu.so /usr/local/lib/python3.12/lib-dynload/xxlimited_35.cpython-312-x86_64-linux-gnu.so
/usr/bin/install -c -m 644 ./Misc/python.man \
        /usr/local/share/man/man1/python3.12.1
if test "xupgrade" != "xno"  ; then \
        case upgrade in \
                upgrade) ensurepip="--altinstall --upgrade" ;; \
                install|*) ensurepip="--altinstall" ;; \
        esac; \
         ./python -E -m ensurepip \
                $ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpf6bqilcw
Requirement already satisfied: pip in /usr/local/lib/python3.12/site-packages (24.0)
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
[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]# python3.12 -c "import ssl,hashlib; print(ssl.OPENSSL_VERSION)"
OpenSSL 1.1.1k  FIPS 25 Mar 2021
[root@iZbp13df0tu0ryucirrwgiZ Python-3.12.3]#

替换python

unlink  /usr/bin/python
unlink  /usr/bin/python3
link   /usr/local/bin/python3.12 /usr/bin/python
link   /usr/local/bin/python3.12 /usr/bin/python3
 
[root@iZbp13df0tu0ryucirrwgiZ ~]# python
Python 3.12.3 (main, Sep  4 2025, 15:53:47) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import ssl
>>>
[root@iZbp13df0tu0ryucirrwgiZ ~]#

 

淘宝小店,欢迎你的光临,可定制你的需求

https://shop340822098.taobao.com/

 image

 

posted @ 2025-09-04 15:59  IT杂物铺  阅读(6)  评论(0)    收藏  举报  来源