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

相关推荐
likfishdn19 分钟前
Linux的文件与目录管理
linux·运维·服务器
渲染101专业云渲染20 分钟前
川翔云电脑优势总结
服务器·3d·电脑·blender·maya
chian-ocean38 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=40 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
橘子味的茶二1 小时前
高级系统架构师--第十章:计算机网络
服务器·计算机网络·系统架构
紫雾凌寒1 小时前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
紫菜(Nori)1 小时前
短连接服务器压测-wrk
服务器·压力测试
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器