如何向PEP 517兼容安装程序指定库与脚本安装目录?
替代Setuptools
setup install指定安装目录的PEP 517兼容方案 问题背景
原Setuptools通过python3 -m setup install --install-lib=/usr/share/foo/ --install-scripts=/usr/share/bar/可指定Python库与可执行脚本的私有安装目录,但该setup install功能已被弃用,需迁移至PEP 517构建系统。官方推荐的build工具无直接对应参数,以下是三种可行替代方案:
方案1:使用pip install直接指定路径
pip作为PEP 517兼容的安装前端,支持直接指定库和脚本的安装位置,完全替代原参数:
pip install . --target=/usr/share/foo/ --install-scripts=/usr/share/bar/
--target对应原--install-lib,指定Python模块的安装目录--install-scripts对应原--install-scripts,指定控制台脚本的安装目录- 优势:无需额外工具,直接兼容PEP 517构建流程,适合快速部署
方案2:使用installer工具(轻量独立安装器)
installer是PEP 600标准的独立安装工具,专门用于安装wheel包,支持精准指定安装路径:
- 先构建wheel包(PEP 517标准产物):
python -m build --wheel
- 用
installer指定路径安装:
installer --install-dir=/usr/share/foo/ --scripts-dir=/usr/share/bar/ dist/*.whl
--install-dir对应原--install-lib--scripts-dir对应原--install-scripts- 优势:无额外依赖,适合自动化部署、容器化场景,完全遵循PEP标准
方案3:通过pip传递配置参数给Setuptools后端
若仍依赖Setuptools作为构建后端,可通过pip的--config-settings参数直接传递原Setuptools安装参数:
pip install . --config-settings=install-lib=/usr/share/foo/ --config-settings=install-scripts=/usr/share/bar/
- 原理:
pip将配置参数传递给Setuptools构建后端,复用原逻辑完成路径指定 - 优势:兼容Setuptools的原有配置习惯,适合需要保留Setuptools特定逻辑的场景
内容的提问来源于stack exchange,提问作者bignose
相关产品推荐
相关产品推荐

