一、为什么 2017 年还要在 CentOS 7 手动配 Python3
CentOS 7 的 yum / firewalld / 大量系统工具强依赖 Python 2.7——直接 yum install python3 装出来的 python3 跟系统的 python(指向 2.7)完全分家,但 2017-2019 那几年的社区包又常常把 2.7 假设写死(典型如 subprocess.Popen 默认行为、字符串编码等)。所以生产环境 Python3 的标准装法是源码编译到 /usr/local/python3,再软链 python3 出去——既不影响系统 python,又方便升级。
到了 2017-2019 几年,Miniconda3 在国内也火了起来——比起源码编译,它的好处是:
- 自带 pip / conda / 大量科学计算包
- 国内源(清华 / 阿里)一配到底
- 环境隔离(
conda create -n py39 python=3.9)
本篇把 Python3 源码编译、Miniconda3 配源、Oh My Zsh 提效这三件事串成一份"开发机一次配好"清单。
本文写于 2017-09-15。示例基于 CentOS 7.9 + Python 3.7 / 3.9 时代。CentOS 7 已 2024-06-30 进入 ELS 阶段,新机器建议 Rocky / Alma 9。
二、Python3 源码编译(与 Python2.7 共存)
2.1 装编译依赖
1
2
3
4
| yum groupinstall "Development Tools" -y
yum install -y openssl-devel bzip2-devel libffi-devel zlib-devel \
ncurses-devel sqlite-devel readline-devel tk-devel \
gdbm-devel db4-devel libpcap-devel xz-devel
|
坑提醒:openssl-devel 一定要装,否则 pip install requests 装好之后跑 HTTPS 会直接报 ssl module not available。
2.2 下载 / 编译 / 安装
1
2
3
4
5
6
7
| wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
tar -zxvf Python-3.9.10.tgz
cd Python-3.9.10
./configure --enable-optimizations --prefix=/usr/local/python3
make altinstall # 注意是 altinstall 不是 install
make && make install
|
./configure --enable-optimizations 会跑一遍 profile-guided optimization(PGO),编译时间长 20-30 分钟但性能提升 10-20%,生产环境值得等。
make altinstall 而不是 make install 关键作用:
- 不覆盖 系统
/usr/bin/python 软链接(系统指向 python2.7) - 不动
/usr/bin/pip - 装出来的
python3.9 / pip3.9 直接在 /usr/local/python3/bin/
2.3 软链接
1
2
3
4
5
6
7
| ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
python3 -V
# Python 3.9.10
pip3 -V
# pip 21.2.4 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)
|
2.4 virtualenv 隔离
为了避免 pip install 污染系统 Python,每个项目单独开 venv:
1
2
3
4
5
6
7
8
| python3 -m venv myenv
source myenv/bin/activate
# 装包
pip install requests flask
# 退出
deactivate
|
三、Miniconda3 + 国内源
3.1 下载安装
1
2
| wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
|
交互流程:
- 按空格看完 license
- 接受 license(输入
yes) - 安装路径(默认
~/miniconda3,回车) - 初始化(
conda init,输入 yes 让它改 ~/.bashrc) - 重开终端生效
3.2 配国内源
~/.condarc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.aliyun.com/anaconda/pkgs/main
- http://mirrors.aliyun.com/anaconda/pkgs/r
- http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.aliyun.com/anaconda/cloud
msys2: http://mirrors.aliyun.com/anaconda/cloud
bioconda: http://mirrors.aliyun.com/anaconda/cloud
menpo: http://mirrors.aliyun.com/anaconda/cloud
pytorch: http://mirrors.aliyun.com/anaconda/cloud
simpleitk: http://mirrors.aliyun.com/anaconda/cloud
|
清华源(https://mirrors.tuna.tsinghua.edu.cn/anaconda/...)也是常用选择,速度一样快。
3.3 配 pip 源
1
2
| pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip config set install.trusted-host mirrors.aliyun.com
|
写到 ~/.config/pip/pip.conf:
1
2
3
| [global]
index-url = https://mirrors.aliyun.com/pypi/simple
trusted-host = mirrors.aliyun.com
|
3.4 常用 conda 命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| # 查版本
conda --version
# 创建隔离环境
conda create -n py39 python=3.9
conda activate py39
# 装包
conda install numpy pandas
# 或 pip(conda 环境里 pip 装也行)
pip install requests
# 退出
conda deactivate
# 列出所有环境
conda env list
# 删除环境
conda env remove -n py39
|
坑提醒:conda 环境和系统 Python 环境完全独立,pip 装的位置也不一样——一个项目只选一个,别混用。
四、Oh My Zsh 提效
bash 是 CentOS 默认 shell,但 zsh 配合 Oh My Zsh 的自动补全、语法高亮、主题,体验好一个档次。2017-2019 几年国内开发者几乎人手一套。
4.1 装 zsh
1
2
3
| yum install -y zsh
zsh --version
# zsh 5.0.2 (x86_64-redhat-linux-gnu)
|
切换默认 shell(要重登录):
4.2 装 Oh My Zsh
官方脚本(国内常被 GFW 拦):
1
| sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
国内镜像版(Gitee):
1
2
3
4
5
6
7
| wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh
vim install.sh
# 把 REMOTE 改成 gitee 镜像
# REMOTE=${REMOTE:-https://gitee.com/${REPO}.git}
chmod +x install.sh
sh install.sh
|
4.3 主题
1
2
3
4
5
6
7
8
9
10
| # 看所有主题
ls ~/.oh-my-zsh/themes
# 改 ~/.zshrc
ZSH_THEME="robbyrussell" # 默认简洁
# 改成
ZSH_THEME="ys" # 国人开发,信息密度高
# 还有很多:agnoster、af-magic、jtriley、bira...
source ~/.zshrc
|
4.4 必装插件
zsh-autosuggestions(灰色提示历史命令):
1
2
3
4
5
6
| git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 国内 Gitee 镜像
git clone https://gitee.com/hailin_cool/zsh-autosuggestions.git \
$ZSH_CUSTOM/plugins/zsh-autosuggestions
|
zsh-syntax-highlighting(命令高亮,错误命令红色):
1
2
3
4
5
6
| git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 国内 Gitee 镜像
git clone https://gitee.com/Annihilater/zsh-syntax-highlighting.git \
$ZSH_CUSTOM/plugins/zsh-syntax-highlighting
|
启用插件——编辑 ~/.zshrc:
1
2
3
4
5
6
7
8
9
10
| # 关键:让 zsh 也能加载 /etc/profile(系统环境变量)
source /etc/profile
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
)
source ~/.zshrc
|
4.5 git 走代理(GFW 友好)
1
2
3
4
5
6
7
8
9
10
11
12
13
| # GitHub 走代理前缀
git config --global url."https://ghproxy.com/https://github.com/".insteadOf "https://github.com/"
# 全局 socks5 代理
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 看所有配置
git config --global --list
|
五、git 提交 master 没有上游分支
第一次 git push 报:
1
| fatal: The current branch master has no upstream branch.
|
设上游:
1
| git push --set-upstream origin master
|
六、典型坑速查
| 现象 | 原因 | 处理 |
|---|
python3 -V 报 command not found | 没装或没软链 | 走 §2.3 软链 |
pip install 报 ssl module not available | 编译时缺 openssl-devel | 重装 Python 加上 --enable-optimizations |
| conda 装包慢 | 没配国内源 | 走 §3.2 配 .condarc |
| Oh My Zsh 安装 git clone 失败 | GFW 拦截 GitHub | 改用 Gitee 镜像脚本 |
| zsh 主题不生效 | 没 source ~/.zshrc | source ~/.zshrc 或重开终端 |
| zsh 找不到系统 PATH 里的命令 | 没 source /etc/profile | 在 ~/.zshrc 第一行加 source /etc/profile |
七、下一步
- 想升级到 Python 3.11+ → 重复 §2,但
make altinstall 后别覆盖 /usr/local/python3 目录里的旧版本 - 想用 pyenv 管多版本 Python →
curl https://pyenv.run | bash,但 pyenv 跟 conda 互不兼容,选一个 - 想给开发机装 Docker → 见
2015-09-15 CentOS 7 全面实战 - 想换到更现代的 shell → fish,但生态不如 zsh 全(很多运维工具默认 profile 写的是 bash/zsh)
参考资料
2024 视角:Python 3.12 / Miniconda py310 / ohmyzsh 已成"标配"
2017 那篇是 Python 3.6/3.7 时代。2024 视角下,Python 已 3.12 LTS,Miniconda 已支持 mamba 加速,ohmyzsh 仍是默认。
一、Python 3.12 LTS 已成主流
- Python 3.10(2021-10):match-case、类型注解增强。
- Python 3.11(2022-10):性能提升 10-60%(CPython 优化)、异常组(ExceptionGroup)。
- Python 3.12(2023-10):f-string 限制取消、类型注解改进、更精确的错误信息。
- Python 3.13(2024-10 计划):JIT 编译器(实验性)、GIL 改进(PEP 703)。
2024 选 Python 版本:
| 场景 | 推荐版本 |
|---|
| 通用开发 | Python 3.12(稳定 + 性能) |
| AI / 机器学习 | Python 3.10/3.11(PyTorch / TensorFlow 兼容) |
| 新项目 | Python 3.12 |
| 维护老代码 | Python 3.8 / 3.9(EOL 2024-10) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 2024 装 Python 3.12
yum install -y openssl-devel bzip2-devel libffi-devel zlib-devel \
ncurses-devel sqlite-devel readline-devel tk-devel \
gdbm-devel db4-devel libpcap-devel xz-devel
wget https://www.python.org/ftp/python/3.12.6/Python-3.12.6.tgz
tar -zxvf Python-3.12.6.tgz
cd Python-3.12.6
./configure --enable-optimizations --prefix=/usr/local/python3
make -j$(nproc)
make altinstall
ln -sf /usr/local/python3/bin/python3.12 /usr/bin/python3.12
ln -sf /usr/local/python3/bin/pip3.12 /usr/bin/pip3.12
|
二、pyenv 已成 Python 版本管理的事实标准
2017 那篇没提 pyenv。2024 主流:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 装 pyenv
curl https://pyenv.run | bash
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
# 装多版本
pyenv install 3.10.14
pyenv install 3.11.9
pyenv install 3.12.6
# 项目级版本
cd myproject
pyenv local 3.12.6
python --version
# Python 3.12.6
|
- 优势:每项目独立 Python 版本,不污染系统。
- vs conda:pyenv + venv 已是 2024 主流(conda 在数据科学领域仍主流)。
三、uv(Rust 写的 Python 包管理器)成为新王
1
2
3
4
5
6
7
8
| # 装(10x 速度于 pip)
curl -LsSf https://astral.sh/uv/install.sh | sh
# 用(替代 pip + venv + pip-tools)
uv venv myenv
source myenv/bin/activate
uv pip install requests flask
# 比 pip 快 10-100 倍
|
- 2024 趋势:新项目大量用
uv,pyproject.toml 标准化。
四、mamba / micromamba 加速 conda
2017 那篇用 Miniconda。2024 conda 仍然慢——推荐 mamba 或 micromamba:
1
2
3
4
5
6
7
8
9
| # 装 micromamba(无需 root)
curl -Ls https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj bin/micromamba
./bin/micromamba shell init -s bash -p ~/micromamba
source ~/.bashrc
# 装包(比 conda 快 5-10 倍)
micromamba create -n py312 python=3.12
micromamba activate py312
micromamba install numpy pandas
|
- 2024 数据科学领域仍以
conda / mamba 为主——科学计算包兼容性最好。
五、poetry / pdm 已成现代 Python 项目管理
- 传统
requirements.txt + pip 已落后。 - 2024 主流:
1
2
3
4
5
6
7
8
9
10
11
12
| # Poetry
curl -sSL https://install.python-poetry.org | python3 -
poetry new myproject
cd myproject
poetry add requests flask
poetry install
# PDM
pip install pdm
pdm init
pdm add requests
pdm install
|
- 优势:自动锁文件(
poetry.lock / pdm.lock)—— 团队每人装出的环境完全一致。 pyproject.toml 标准化(PEP 621)。
六、Oh My Zsh 的"接班人"
2017 那篇是 ohmyzsh。2024 视角:
ohmyzsh 仍在用,但有更轻量替代:
- Zim(启动快 3 倍)
- Zinit(按需加载)
- Oh My Zsh + 主题精简(去掉 90% 不用的功能)
2024 趋势:
- Starship(Rust 写的 prompt)—— 跨 shell 统一,比 ohmyzsh 默认 prompt 快 10 倍
- Atuin(Rust 写的 shell history)—— 跨机器同步历史命令、全文搜索
1
2
3
4
5
| # 装 Atuin
curl --proto '=https' --tlsv1.2 -sSf https://setup.atuin.sh | sh
atuin login
atuin sync
# 跨机器同步历史命令
|
七、AI 时代的 Python 生态
- 2024 大量新 Python 项目是 AI / LLM 应用:
- LangChain(LLM 编排)
- LlamaIndex(RAG)
- Haystack(NLP 框架)
- vLLM(LLM 推理)
- PyTorch / TensorFlow(深度学习)
- Transformers(Hugging Face)
1
2
3
4
| # 2024 AI 项目的"开发机配置"
micromamba create -n llm python=3.10
micromamba activate llm
pip install torch transformers vllm
|
八、CentOS 7 → Rocky 9 的 Python 迁移
- CentOS 7 默认 Python 2.7(已 EOL)。
- Rocky 9 默认 Python 3.9(2024 已 EOL,需升 3.12)。
- 迁移要点:
- 不要直接覆盖系统
python(影响 yum / dnf) - 用
pyenv 或 conda 装应用 Python - 脚本 shebang:
#!/usr/bin/python → #!/usr/bin/python3 - pip 2 完全不可用