一、在线安装python3
# centos等基于redhat系统
yum install python3 -y
# ubuntu等基于Debian系统
apt install python3 -y
二、conda 包管理器安装(推荐)
# 安装过程有交互步骤,需要阅读许可声明,确定安装目录
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && chmod +x Miniconda3-latest-Linux-x86_64.sh && ./Miniconda3-latest-Linux-x86_64.sh
source ~/miniconda3/bin/activate
列出可用的python版本
conda search "^python$"
创建conda环境,并安装配置python版本
conda create -n pyenv 3.10 python=3.10.14
激活环境
conda activate pyenv3.10
在conda 环境中更新 或修改 python 版本
conda update python # 更新
conda install python=3.12.4 # 安装其他版本
列出所有的conda 环境
conda env list
删除环境
conda env remove -n pyenv3.8.5
3.源码编译安装
通过 PYTHON_VER 指定版本
本脚本下载python安装包使用的是华为云源地址 https://repo.huaweicloud.com/python
脚本内容如下:
#! /bin/bash
PYTHON_VER=3.10.2
SOURCE_DIR=/opt/source
function detect_pip_version {
pip3 -V
if [ $? -eq 0 ];then
echo "已存在pip3"
else
echo "pip3不存在,即将安装pip3"
return 1
fi
}
function deploy_python3 {
echo "安装基础依赖"
sudo yum groupinstall -y "Development Tools"
sudo yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel readline-devel sqlite-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
if [ $? -ne 0 ];then
echo "基础依赖安装失败"
exit 1
fi
echo "下载Python安装包"
if [ ! -d $SOURCE_DIR ];then
mkdir -p $SOURCE_DIR
fi
wget -V
if [ $? -ne 0 ];then
yum install wget -y
fi
sudo wget https://mirrors.huaweicloud.com/python/$PYTHON_VER/Python-$PYTHON_VER.tgz -P $SOURCE_DIR
sudo tar -zxvf $SOURCE_DIR/Python-$PYTHON_VER.tgz -C $SOURCE_DIR
cd $SOURCE_DIR/Python-$PYTHON_VER
if [ $? -ne 0 ];then
echo "安装包下载或解压出现异常"
exit 1
fi
echo "配置python"
sudo ./configure --prefix=/usr/local/python$PYTHON_VER
if [ $? -ne 0 ];then
echo "环境配置出现异常"
exit 1
fi
echo "编译、安装"
sudo make -j $(nproc) && sudo make install
if [ $? -ne 0 ];then
echo " 编译安装出现异常"
exit 1
fi
echo " 配置python相关的环境变量 "
echo "export PYTHON_HOME=/usr/local/python$PYTHON_VER" >> /etc/profile
echo "export PATH=\$PYTHON_HOME/bin:\$PATH" >> /etc/profile
source /etc/profile
pip3 -V
if [ $? -ne 0 ];then
echo "python$PYTHON_VER 安装失败"
exit 1
else
echo "python$PYTHON_VER 安装完成"
fi
}
detect_pip_version
if [ $? -eq 1 ];then
deploy_python3
su - $USER
fi