Skip to content
Search CentOS Stream

This page uses Chinese content. Search opens the Chinese documentation.

    Browse this documentation

    安装与选择 Python 版本

    This content is not available in your language yet.

    适用:CentOS Stream 9 / 10 · 核对日:2026-11-01 · 官方依据:仓库内包元数据(dnf repoquery)与 dnf 自身输出,全部在容器实测。

    Python 是 Stream 上最容易踩版本坑的运行时:默认版本不许动,新版本用并行包,模块流在 Stream 10 上已经消失。本页结论全部来自容器实测(Stream 9 镜像 sha256:4fdfb36…,Stream 10 stream10 镜像,2026-11-01)。

    查看系统默认 Python
    $ python3 --version
    $ dnf repoquery --qf '%{name}-%{version}' python3

    预期输出(实测摘录):

    # Stream 9
    Python 3.9.25
    python3-3.9.25
    # Stream 10
    Python 3.12.12
    python3-3.12.12

    并行版本:装一个不干扰系统的新 Python

    Section titled “并行版本:装一个不干扰系统的新 Python”

    两代 Stream 都以独立包提供较新的解释器。先看仓库里有哪些:

    查可用的并行版本包
    $ dnf repoquery --qf '%{name}-%{version}' 'python3.*' | sort -u

    实测摘录:

    # Stream 9 可装
    python3.11-3.11.13
    python3.12-3.12.13
    # Stream 10 可装
    python3.14-3.14.3

    安装与验证(以 Stream 9 装 3.12 为例):

    安装并验证并行版本
    $ sudo dnf install python3.12
    $ python3.12 --version

    预期输出:

    Python 3.12.13

    并行包的命令名带版本号(python3.12),默认 python3 仍指向系统版本,dnf 不受影响。回退即 sudo dnf remove python3.12。

    模块流不是新版本的入口。如果你照着旧教程执行 dnf module enable python3x:

    • Stream 9:模块列表里没有 Python 条目(实测 24 条模块记录里无 python);
    • Stream 10:直接报错——模块化内容已清空,dnf module list 为 0 条,且 dnf 4.20.0 打出弃用警告:
    WARNING: modularity is deprecated, and functionality will be removed in a future release of DNF5.

    这条差异也是报错速查里的常客:把 RHEL 8 / CentOS 8 时代的模块化教程搬来 Stream 10 用,就会撞上它。

    用并行版本建虚拟环境
    $ python3.12 -m venv ~/venvs/myproj
    $ source ~/venvs/myproj/bin/activate
    (myproj) $ python --version && pip --version

    预期:环境内 python 即 3.12.x,pip 装的包只进虚拟环境目录。系统 RPM Python 包(dnf install python3-requests 这类)与虚拟环境互不可见——需要系统级 RPM 包时用 dnf 装,项目依赖用 pip 装,两层不要混写。

    • 并行版本:sudo dnf remove python3.12,不留残留;
    • 虚拟环境:删除环境目录即可(rm -rf ~/venvs/myproj);
    • 默认 Python 不可回滚也不可升级——如果你已经动过它,从安装介质/容器重新核对 python3 包版本并恢复原包。
    现象原因处理
    dnf module enable python3x 报错Stream 9 无 Python 模块;Stream 10 模块化已移除用并行版本包 python3.11/3.12/3.14
    pip 装的包与 dnf 装的冲突同一 site-packages 混写项目内用 venv,系统级只用 RPM
    编译扩展报找不到 Python 头文件缺开发包dnf install python3.12-devel(对应版本)
    python3 版本和文档对不上跟随 RHEL 点版本滚动以 dnf repoquery python3 实测为准,注意核对日