[Centos] Install the python environment (a script compiles and installs any version)

foreword

A few days ago, because the python environment was installed on Ubuntu, I wrote a script that can compile and install any python version.

[Ubuntu] Install the python environment (a script compiles and installs any version)

Then take it to the centos7 system and try it out to see if it can be used universally, but an error is reported. Later, after investigation, it was found that the reason why the script failed was that the environment dependencies for compiling and installing python in the centos system were somewhat different from those in the Ubuntu system, so the script was modified. Students in need can pick it up by themselves.

Centos7 install python environment

Create a new install_python.sh file

vi install_python.sh

Edit the install_python.sh file and fill in the following content. The following python version numbers can be customized. What are the specific version numbers of python, you can check the link below

https://www.python.org/ftp/python/

Here I installed python3.7.9 version

#!/bin/bash
# by baiyu 2021 攻城狮白玉
#-----可变参数-start-----
# 要下载的python版本
# python的大版本号
py_version=3.7
# python的具体版本号
version=$py_version.9
# 要安装的路径
install_path=/usr/local/src/python37
#-----可变参数-end-----

echo -e '即将安装python$version'
echo -e '安装路径为$install_path'

# 安装依赖以及升级索引
yum -y groupinstall "Development tools"
yum install -y zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

yum update -y
yum upgrade -y

# 创建安装目录文件夹
mkdir -p $install_path

# 下载python
echo -e '正在下载'
# 使用官方网址下载--速度可能比较慢
# wget https://www.python.org/ftp/python/$version/Python-$version.tgz
# 使用国内华为镜像源下载python
wget https://mirrors.huaweicloud.com/python/$version/Python-$version.tgz
echo -e "正在解压"
# 静默解压
tar -xzf Python-$version.tgz
# 删除压缩包
echo -e "解压完成,移除压缩包"
rm -rf Python-$version.tgz

echo -e "正在安装"
cd Python-$version
./configure --prefix=$install_path  # 配置安装位置
make
make install

echo -e "配置软连接"
rm -rf /usr/bin/python$py_version /usr/bin/pip$py_version
ln -s $install_path/bin/python$py_version /usr/bin/python$py_version
ln -s $install_path/bin/pip$py_version /usr/bin/pip$py_version

cd ..
rm -rf Python-$version
echo -e "完成安装Python-$version"

You can modify the major version number and specific version number corresponding to the script according to your needs. For example, if you want to install python3.9.1 version, modify the version number as follows:

py_version=3.9  

version=$py_version.1

Use the chmod command to add executable permissions and execute the script

chmod +x install_python.sh
./install_python.sh

After waiting for the installation, you can type python3.8 on the command line to directly enter the python environment

Summarize

This script compiles and installs the python environment under centos7, and will not conflict with the original python environment.

After the installation is complete, enter the installed version number on the command line to enter the corresponding python environment.

written in the back

If you find it useful, please support Siege Lion Baiyu with one button and three consecutive links , and share this article with more friends. Your simple support, my infinite creative power

 

Guess you like

Origin blog.csdn.net/zhh763984017/article/details/119915082