-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython3.9.sh
More file actions
executable file
·73 lines (60 loc) · 2.02 KB
/
python3.9.sh
File metadata and controls
executable file
·73 lines (60 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
# 颜色常量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# Python 版本号
PYTHON_VERSION="3.9.6"
echo -e "${GREEN}开始安装 Python ${PYTHON_VERSION}${NC}"
# 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}请使用 root 用户运行此脚本${NC}"
exit 1
fi
# 卸载本地 Python 版本
echo -e "${YELLOW}卸载本地 Python 版本...${NC}"
yum remove python3 -y
# 安装依赖包
echo -e "${YELLOW}安装依赖包...${NC}"
yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel sqlite-devel -y
# 下载 Python 源码
PYTHON_URL="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"
echo -e "${YELLOW}下载 Python 源码...${NC}"
wget "${PYTHON_URL}"
# 解压并编译安装
echo -e "${YELLOW}解压并编译安装 Python...${NC}"
tar -xvf "Python-${PYTHON_VERSION}.tgz"
cd "Python-${PYTHON_VERSION}"
./configure --enable-optimizations
make
make altinstall
# 验证 Python 版本
echo -e "${GREEN}Python 版本:${NC}"
python3.9 --version
# 配置 .bashrc 文件
echo -e "${YELLOW}配置 .bashrc 文件...${NC}"
if ! grep -q "alias python3=\"/usr/local/bin/python3.9\"" ~/.bashrc; then
echo "alias python3=\"/usr/local/bin/python3.9\"" >> ~/.bashrc
fi
if ! grep -q "alias pip3=\"/usr/local/bin/pip3.9\"" ~/.bashrc; then
echo "alias pip3=\"/usr/local/bin/pip3.9\"" >> ~/.bashrc
fi
source ~/.bashrc
# 验证 Python 和 pip 路径
echo -e "${GREEN}Python 路径:${NC}"
which python3
echo -e "${GREEN}pip 路径:${NC}"
which pip3
# 设置 Python 清华代理源
echo -e "${YELLOW}设置 Python 清华代理源...${NC}"
mkdir -p ~/.pip
if ! grep -q "\[global\]" ~/.pip/pip.conf; then
echo "[global]" >> ~/.pip/pip.conf
echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> ~/.pip/pip.conf
fi
if ! grep -q "\[install\]" ~/.pip/pip.conf; then
echo "[install]" >> ~/.pip/pip.conf
echo "trusted-host = https://pypi.tuna.tsinghua.edu.cn" >> ~/.pip/pip.conf
fi
echo -e "${GREEN}Python ${PYTHON_VERSION} 安装成功!${NC}"