在 CentOS 上配置 Python 时,有几个容易踩坑但非常重要的注意点。下面按「系统层面 → 安装方式 → 使用建议」给你梳理。
CentOS(尤其是 7)中:
/usr/bin/python → Python 2(系统依赖)yum、firewalld、systemctl 等都依赖它❌ 错误做法:
rm /usr/bin/python
ln -s /usr/bin/python3 /usr/bin/python
这会导致 yum 直接挂掉。
✅ 正确做法:
python3 命令调用新版 Python| CentOS 版本 | 默认 Python |
|---|---|
| CentOS 7 | Python 2.7 |
| CentOS 8 | Python 3.6(但已 EOL) |
| CentOS Stream | Python 3.9+ |
dnf / yum(稳定)yum install python3
或(CentOS 8+):
dnf install python3
⚠ 注意:
yum groupinstall "Development Tools"
yum install openssl-devel bzip2-devel libffi-devel zlib-devel
wget https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tgz
tar xzf Python-3.11.6.tgz
cd Python-3.11.6
./configure --enable-optimizations
make && make altinstall
⚠ 一定要用:
make altinstall
而不是 make install,避免覆盖系统 Python。
pyenv(开发推荐)适合多版本共存:
curl https://pyenv.run | bash
pyenv install 3.11.6
pyenv global 3.11.6
python3 -m pip install xxx
不要用:
pip install xxx
(容易装到 Python 2)
python3 -m venv venv
source venv/bin/activate
避免:
❗ openssl 版本问题
✅ 解决:
❗ locale 问题
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
❗ 防火墙 & SELinux
systemd 管理 Python 服务gunicorn / uwsgi如果你能告诉我:
我可以给你一套精准配置方案。