ModuleNotFoundError:没有名为 'distutils' 的模块
ModuleNotFoundError: No module named 'distutils' File "setup.py", line 3, in <module> from distutils.core import setup
ID: python/setuptools-distutils-removed-python-312
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.12 | active | — | — | — |
| 3.13 | active | — | — | — |
根因分析
Python 3.12 移除了标准库 `distutils` 模块(PEP 632)。旧版 setup.py 和 setuptools(<60)会直接导入它,导致在 3.12+ 上构建或安装失败。
English
Python 3.12 removed the stdlib `distutils` module (PEP 632). Legacy setup.py files and old setuptools versions (<60) import it directly, so building or installing the package fails on 3.12+.
解决方案
-
90% 成功率
Upgrade setuptools to >=60, which vendors a distutils shim: pip install --upgrade "setuptools>=60" Then rebuild with `python -m build` or `pip install .`.
-
80% 成功率
Install the standalone backport for tools that still need the real distutils: pip install setuptools pip install --upgrade pip # or, for legacy code: pip install setuptools==69.5.1 # last version with full shim For projects you control, migrate setup.py to pyproject.toml with the `[build-system]` table.
无效尝试
常见但无效的做法:
-
95% 失败
There is no PyPI package named `distutils`; the module was stdlib-only. pip reports 'No matching distribution found'.
-
60% 失败
setuptools' distutils shim is only installed when setuptools>=60 is present and even then some submodules (distutils.util, distutils.sysconfig) are not fully emulated.