为何numpy 1.25.1不支持PEP517构建?安装遇distutils缺失错误
问题
执行poetry install安装numpy==1.25.1时触发ChefBuildError,提示后端setuptools.build_meta:legacy不可用,核心错误如下:
ModuleNotFoundError: No module named 'distutils' at /opt/homebrew/Cellar/poetry/1.7.1/libexec/lib/python3.12/site-packages/poetry/installation/chef.py:164 in _prepare
Poetry提示错误源于构建后端,推测numpy 1.25.1不支持PEP517构建。已尝试通过Homebrew重装Python3.12,无效;按Poetry建议执行pip wheel --no-cache-dir --use-pep517 "numpy (==1.25.1)",仍出现distutils缺失的ModuleNotFoundError。
运行环境
System Version: macOS 14.1.2 (23B92) Kernel Version: Darwin 23.1.0 Homebrew: 4.1.22 Poetry: 1.7.1 Python: 3.12.0 pip: 23.3.1
解决方案
1. 安装distutils模块
Python3.12默认移除了distutils,需单独安装:
brew install python-setuptools
安装后确认distutils已关联到当前Python环境。
2. 强制numpy使用传统构建后端
在项目的pyproject.toml中添加以下配置,绕过PEP517限制:
[tool.poetry.build-system] requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta:legacy"
或者临时通过环境变量指定:
POETRY_BUILD_BACKEND=setuptools.build_meta:legacy poetry install
3. 降级Python版本(备选方案)
若上述方法无效,降级到Python3.11(默认包含distutils,且numpy 1.25.1支持更完善):
brew install python@3.11 poetry env use python3.11 poetry install
内容的提问来源于stack exchange,提问作者Brian Nguyen
相关产品推荐
相关产品推荐

