
Python环境初始化编译安装打包迁移配置pip国内源仓库环境初始化基于CentOS7.9联网环境下编译安装python3.12稳定版本配置本地离线源虚拟机环境且挂载了镜像mount/dev/sr0 /mnt/mv/etc/yum.repos.d/* /home/cat/etc/yum.repos.d/c.repoeof [c] namec baseurlfile:///mnt gpgcheck0 enabled1 eofyum clean all;yum repolist如果没有挂载可以使用阿里源curl-o/etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo安装编译所需依赖yum groupinstallDevelopment Tools-yyuminstall-y\gcc\zlib-devel\ncurses-devel\gdbm-devel\openssl-devel\sqlite-devel\readline-devel\tk-devel\libffi-devel\bzip2-devel\xz-devel\libcurl-devel\db4-devel\perl-IPC-Cmd\wget编译安装在CentOS 7上编译安装 Python 3.12 时由于系统自带的 OpenSSL 版本较低可能会导致 Python 无法使用最新的 SSL/TLS 功能。为了解决这个问题需要先安装一个较新版本的 OpenSSL官网wgethttps://github.com/openssl/openssl/releases/download/openssl-3.4.1/openssl-3.4.1.tar.gztar-xfopenssl-3.4.1.tar.gzcdopenssl-3.4.1开始编译安装./config--prefix/usr/local/openssl--openssldir/usr/local/openssl shared[rootlocalhost openssl-3.4.1]# ./config --prefix/usr/local/openssl --openssldir/usr/local/openssl sharedConfiguring OpenSSL version3.4.1fortarget linux-x86_64 Using os-specific seed configuration Created configdata.pm Running configdata.pm Created Makefile.in Created Makefile Created include/openssl/configuration.h ********************************************************************** *** *** *** OpenSSL has been successfully configured *** *** *** *** If you encounter a problemwhilebuilding, pleaseopenan *** *** issue on GitHubhttps://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 *** ***Troubleshootingsectioninthe INSTALL.mdfilefirst)*** *** *** **********************************************************************make;makeinstall设置环境变量vi~/.bashrcexportLDFLAGS-L/usr/local/openssl/lib64 -Wl,-rpath,/usr/local/openssl/lib64exportCPPFLAGS-I/usr/local/openssl/includeexportLD_LIBRARY_PATH/usr/local/openssl/lib64:$LD_LIBRARY_PATHexportPKG_CONFIG_PATH/usr/local/openssl/lib64/pkgconfig:$PKG_CONFIG_PATHsource~/.bashrc查看版本如果需要重新编译删除/usr/local/openssl文件执行make clean即可[rootlocalhost openssl-3.4.1]# /usr/local/openssl/bin/openssl versionOpenSSL3.4.111Feb2025(Library: OpenSSL3.4.111Feb2025)官网下载文档版本Python3.12如果命令下载失败去网页下载然后上传wgethttps://www.python.org/ftp/python/3.12.9/Python-3.12.9.tar.xztar-xfPython-3.12.9.tar.xzcdPython-3.12.9配置 Python 使用自定义 OpenSSL./configure--prefix/usr/local/python3.12\--with-openssl/usr/local/openssl\--with-openssl-rpathautomake;makeinstall创建软链接ln-sf/usr/local/python3.12/bin/python3.12 /usr/bin/python3ln-sf/usr/local/python3.12/bin/pip3.12 /usr/bin/pip3验证查看[rootlocalhost Python-3.12.9]# python3 -c import ssl; print(ssl.OPENSSL_VERSION)OpenSSL3.4.111Feb2025./configure 命令的常见选项说明–prefix指定 Python 安装的目录。–enable-optimizations启用优化通常会增加编译时间但能生成更高效的二进制文件。–enable-shared构建共享库版本的 Python适用于需要 Python 动态链接的环境。–with-ensurepipinstall安装 pipPython 包管理工具。install 表示安装 pipupgrade 表示升级已安装的 pipnone 表示不安装。–with-ssl启用 OpenSSL 支持允许 Python 使用 SSL 加密。–with-dbmliborder指定用于数据库支持的库如 gdbm 或 dbm。–with-readline启用 Readline 支持提供命令行输入的历史记录和编辑功能。–with-zlib启用对 zlib 压缩库的支持通常用于对大型文件进行压缩和解压。–with-lto启用链接时优化LTO提升代码性能但增加编译时间。–enable-unicodeucs4 / --enable-unicodeucs2设置 Python 中的 Unicode 字符串实现为 UCS4 或 UCS2适用于不同的字符集大小。–disable-ipv6禁用 IPv6 支持减少不必要的网络功能。–enable-loadable-sqlite-extensions启用 SQLite 可加载扩展支持。–with-pymalloc启用 pymalloc 分配器优化内存管理通常可以提高性能。–without-ensurepip禁用 pip 安装。–without-threads禁用 Python 的线程支持。–with-bz2启用对 bzip2 的支持。–with-uuid启用 UUID 支持。–with-xxhash启用 xxhash 支持这是一种快速的哈希算法。–disable-test-modules禁用 Python 的测试模块。–enable-pydebug启用调试支持增加调试信息适用于开发和调试阶段。–disable-shared禁用构建共享库版本的 Python。–with-system-expat使用系统安装的 expat 库而不是 Python 内部的 expat 实现。–without-gcc禁用使用 GCC 编译 Python如果使用其他编译器时可能会用到。打包迁移如果需要将本地配置好的python环境打包迁移到其他相同的系统上使用包括whl依赖等源机器上操作# 1. 把 OpenSSL 的 .so 文件复制到 Python 安装目录cp/usr/local/openssl/lib64/libcrypto.so* /usr/local/python3.12/lib/cp/usr/local/openssl/lib64/libssl.so* /usr/local/python3.12/lib/# 2. 确保 libpython3.12.so 也在如果编译时没加 --enable-shared 需要重新编译ls/usr/local/python3.12/lib/libpython3.12.so# 3. 打包整个 Python 目录单目录所有依赖都在里面cd/tar-czpf/tmp/python3.12_all_in_one.tar.gz /usr/local/python3.12目标机器tar-xzf/tmp/python3.12_all_in_one.tar.gz-C/ln-sf/usr/local/python3.12/bin/python3.12 /usr/bin/python3ln-sf/usr/local/python3.12/bin/pip3.12 /usr/bin/pip3echo/usr/local/python3.12/lib/etc/ld.so.conf.d/python3.12.conf ldconfig python3--version配置pip国内源仓库如果没有安装pip手动安装 pip下载 get-pip.pycurlhttps://bootstrap.pypa.io/get-pip.py-oget-pip.py或使用浏览器下载https://bootstrap.pypa.io/get-pip.py然后运行python get-pip.py更新 pippython-mpipinstall--upgradepip解决安装依赖失败问题使用国内源阿里源mkdir~/.pip/cat~/.pip/pip.confEOF [global] index-url http://mirrors.aliyun.com/pypi/simple/ [install] trusted-hostmirrors.aliyun.com EOF配置清华源pip临时使用注意simple 不能少。pip 要求使用 https 因此需要 https 而不是 httppipinstall-ihttps://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple some-package设为默认升级 pip 到最新的版本后进行配置python-mpipinstall--upgradepip pip configsetglobal.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple