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

1.下载官网Python安装包

wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tar.xz

1.1解压

tar -xf Python-3.12.2.tar.xz

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

cd /usr/packages/Python-3.12.2/

1.2升级软件包管理器

CentOS系统:

yum update

Ubuntu系统

 sudo apt-get update

1.3配置

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

–prefix :指定安装路径

–enable-optimizations: 用来启动优化

./configure --prefix=/usr/local/python3.12 --enable-optimizations

 1.4编译

make负责生成目标文件

make

 1.5安装

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

make install

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

/usr/local/python3.12/bin/python3 --version

运行结果 

Python 3.12.2

1.7验证ssl_ssl是否安装成功

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 "", line 1, in 
  File "/usr/local/python3.12/lib/python3.12/ssl.py", line 100, in 
    import _ssl             # if we can't import it, let the error propagate
    ^^^^^^^^^^^
ModuleNotFoundError: No module named '_ssl'

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

python退出:

quit()

1.8安装openssl库

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

CentOS

yum install openssl-devel

Ubuntu 

apt-get install libssl-dev

查看 openssl是否安装成功

openssl version

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

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

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

[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建立软链接 

ln -s /usr/local/python3.12/bin/python3.12 /usr/bin/python3.12
ln -s /usr/local/python3.12/bin/pip3.12 /usr/bin/pip3.12
ln -s /usr/local/python3.12/bin/python3.12-config /usr/bin/python3.12-config

2.2配置pip镜像源

注意我这里用的是pip3.12

查看当前镜像源

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

pip3.12 config list

配置pip镜像源

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

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

pip3.12 install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

 永久使用

pip3.12 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

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

mkdir ~/.pip
vi ~/.pip/pip.conf

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

阿里镜像源

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

清华镜像源

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

 测试pip是否正常

我这里用的是pip3.12

pip3.12 install requests
[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

你可能感兴趣的:(linux,运维,服务器,python)