Linux (centos) install Python3.9 (nanny level)

Insert image description here

Deployment Guide

The project requires a Python environment to run. Python 3 or above is recommended. The author uses Python 3.9.7. The following are the installation steps: Pay
special attention to the fact that most Pythons shipped with Linux are version 2.7.5. If we want to use Python3, it is best to install a new Python3 environment, but please do not try to delete Python2 to avoid unnecessary trouble.

1. Update the system software package (you can use the default yum configuration of centos7):

yum update

2. Install the dependencies required to compile Python:

yum install -y git gcc make openssl-devel bzip2-devel libffi-devel zlib-devel readline-devel sqlite-devel

Note at this time: When python3 installs requests, the requests package introduces urllib3, and the new version of urllib3
requires OpenSSL 1.1.1+ or above, otherwise an error will be reported. The default OpenSSL version is OpenSSL 1.0.2k-fips 26 Jan 2017.
Installing OpenSSL 1.1.1+ or above requires recompiling and installing python to take effect, so this step is here.

Solution: Need to upgrade openssl, download and compile openssl

# 下载openssl-1.1.1t源代码包:
wget --no-check-certificate   https://www.openssl.org/source/openssl-1.1.1t.tar.gz
# 解压
tar -zxvf openssl-1.1.1t.tar.gz
cd openssl-1.1.1t/
# 指定openssl安装的目标路径
./config --prefix=/usr/local/my_openssl
# 在CPU占用不多的情况下,可以适当使用4个线程加速编译,可以根据需要调整线程数,
make # make -j4
make install

3. Install python:

# 下载python3.9.7源代码包
wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
# 解压源代码包
tar -xf Python-3.9.7.tgz
# 进入源代码目录
cd Python-3.9.7
# 配置编译参数
./configure --enable-optimizations --with-openssl=/usr/local/my_openssl #把openssl安装路径配置到编译参数中
# 如果出现Could not import runpy module的报错,那么说明gcc版本太低,不支持--enable-optimizations参数,把它去掉就好
# 编译并安装Python
make
make altinstall
# 此时python安装完毕,但是由于附带了2.7.5版本的Python,所以此时查看Python的版本仍是2.7.5
# 查看python3的版本
python3 --version

At this point, python3 is installed.
We can specify the paths of python and pip commands by setting soft connections so that they use python3 and pip3. However, considering other effects, it is better not to replace these two commands at will.

4. Set up pip mirror source

# 检查当前的pip配置
pip3 config list
# 设置pip安装源:以设置清华大学镜像为例
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 验证配置是否成功:你可以再次运行以下命令来检查配置是否正确生效:
pip3 config list

5. Install virtual environment parser

During the deployment of the python project, we need to configure a parser for it. We can use the virtualenv tool to create a virtual environment. Of course, we can also use other

# 安装 virtualenv
pip3 install virtualenv
# 使用 virtualenv 创建虚拟环境
virtualenv -p python3 my-virtualenv

After creating the virtual environment, you can see that a new my-virtualenv directory has been created in the current directory, and this directory can be used as a parser. Because such a project will be created, make sure that there is no my-virtualenv directory before creating a virtual environment.

# 激活虚拟环境
source my-virtualenv/bin/activate
# 使用完虚拟环境后退出
deactivate

Guess you like

Origin blog.csdn.net/rock1112uhhgg/article/details/131938042