linux(CentOS、Ubuntu)安装python3.12.2环境

1.下载官网Python安装包

python 复制代码
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tar.xz

1.1解压

python 复制代码
tar -xf Python-3.12.2.tar.xz

解压完后切换到Python-3.12.2文件夹(这里根据自己解压的文件夹路径)

bash 复制代码
cd /usr/packages/Python-3.12.2/

1.2升级软件包管理器

CentOS系统:

bash 复制代码
yum update

Ubuntu系统

bash 复制代码
 sudo apt-get update

1.3配置

configure的用途:检测安装环境,配置安装参数,生成供编译用的Makefile

--prefix :指定安装路径

--enable-optimizations: 用来启动优化

bash 复制代码
./configure --prefix=/usr/local/python3.12 --enable-optimizations

1.4编译

make负责生成目标文件

bash 复制代码
make

1.5安装

make install:负责把编译生成的目标文件安装到预定的目录

bash 复制代码
make install

1.6安装完后查看是否安装成功

bash 复制代码
/usr/local/python3.12/bin/python3 --version

运行结果

bash 复制代码
Python 3.12.2

1.7验证ssl_ssl是否安装成功

bash 复制代码
root@iZ2zecntz3no31t5bob80pZ:/usr/packages/Python-3.12.2# /usr/local/python3.12/bin/python3
Python 3.12.2 (main, Jul 18 2024, 19:03:05) [GCC 11.4.0] 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/python3.12/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'

可以看到报模块没有找到的异常

python退出:

bash 复制代码
quit()

1.8安装openssl库

注意这里linux系统默认安装的有openssl,版本1.1.1以上,但是不清楚什么原因自带的openssl无法使python正常编译,因此需要再次安装

CentOS

bash 复制代码
yum install openssl-devel

Ubuntu

bash 复制代码
apt-get install libssl-dev

查看 openssl是否安装成功

bash 复制代码
openssl version

1.9重新配置、编译、安装python3.12.2

安装完openssl后重新配置、编译、安装python3.12.2,和1.3~1.5步骤一样,这里不再赘述

安装完python3.12.2后,再次验证验证ssl_ssl是否安装成功

bash 复制代码
[root@localhost ~]# /usr/local/python3.12/bin/python3.12
Python 3.12.2 (main, Jul 19 2024, 11:32:16) [GCC 8.5.0 20210514 (Red Hat 8.5.0-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> import _ssl
>>> 

没有报错,证明ssl_ssl安装成功

2.创建软链接、配置pip镜像源

2.1建立软链接

bash 复制代码
ln -s /usr/local/python3.12/bin/python3.12 /usr/bin/python3.12
bash 复制代码
ln -s /usr/local/python3.12/bin/pip3.12 /usr/bin/pip3.12
bash 复制代码
ln -s /usr/local/python3.12/bin/python3.12-config /usr/bin/python3.12-config

2.2配置pip镜像源

注意我这里用的是pip3.12

查看当前镜像源

默认是python官方镜像源,没有配置镜像源的情况下,查看为空

bash 复制代码
pip3.12 config list

配置pip镜像源

方式一(通过命令更换源):

临时使用,这里已安装requests为例 -i 后面是临时镜像源地址

bash 复制代码
pip3.12 install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

永久使用

bash 复制代码
pip3.12 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

****方式二(通过配置文件的形式更换源):

bash 复制代码
mkdir ~/.pip
vi ~/.pip/pip.conf

填入以下内容(任选其一,也可选其他镜像源)

阿里镜像源

bash 复制代码
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

清华镜像源

bash 复制代码
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

测试pip是否正常

我这里用的是pip3.12

bash 复制代码
pip3.12 install requests
bash 复制代码
[root@localhost ~]# pip3.12 install requests
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting requests
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f9/9b/335f9764261e915ed497fcdeb11df5dfd6f7bf257d4a6a2a686d80da4d54/requests-2.32.3-py3-none-any.whl (64 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.9/64.9 kB 2.3 MB/s eta 0:00:00
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/python3.12/lib/python3.12/site-packages (from requests) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/python3.12/lib/python3.12/site-packages (from requests) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/python3.12/lib/python3.12/site-packages (from requests) (2.2.2)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/python3.12/lib/python3.12/site-packages (from requests) (2024.7.4)
Installing collected packages: requests
Successfully installed requests-2.32.3
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: 24.0 -> 24.1.2
[notice] To update, run: python3.12 -m pip install --upgrade pip

linux 部署flask项目 : https://blog.csdn.net/weixin_41934979/article/details/140614329

参考资料

https://blog.csdn.net/weixin_43881017/article/details/134656575

https://blog.csdn.net/Amio_/article/details/126716818

https://blog.csdn.net/weixin_42115825/article/details/134923785

https://blog.csdn.net/JineD/article/details/125090904

相关推荐
GIS数据转换器4 分钟前
智慧灌区管理平台
大数据·服务器·网络·数据库·人工智能·生活
STLearner1 小时前
ICML 2026 | LLM×Graph论文总结[2]【Graph4LLM,Graph4Agent,智能体记忆(Memory)
大数据·人工智能·python·深度学习·学习·机器学习·数据挖掘
寒水馨2 小时前
Linux下载、安装 Codex CLI(附安装包codex-x86_64-unknown-linux-musl.tar.gz)
linux·openai·终端·ai编程·codex·智能体·codex cli
其实防守也摸鱼2 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·运维·网络·人工智能·学习·安全·macos
郭老二10 小时前
【Python】基本语法:装饰器语法糖@
python
冰封之寂10 小时前
Docker 资源管理终极指南:CPU、内存与 IO 的精细化控制
linux·运维·服务器·docker·云计算
xiaoye-duck11 小时前
《Linux系统编程》Linux 系统多线程(五):<线程同步与互斥>线程互斥(上):从条件变量到生产者消费者模型详解
linux·线程
_Jimmy_11 小时前
Agent常用检索器的详细介绍
python·langchain
小柯南敲键盘11 小时前
图片翻译API接入与自动化实现指南
运维·python·自动化
旅僧12 小时前
Q-learning(自用)
python·机器学习