保留pyproject.toml时pip install -e .无法构建开发版的解决办法
保留pyproject.toml时如何完成
pip install -e .的开发版构建? 问题背景
当项目中存在仅包含black代码格式化配置的pyproject.toml时,执行pip install -e .不会触发传统的Running setup.py develop流程;删除该文件后,才能正常完成开发版安装。需要在保留pyproject.toml的前提下实现开发版构建。
相关配置文件
pyproject.toml内容
# Source https://github.com/psf/black#configuration-format [tool.black] line-length = 100 target-version = ['py38'] exclude = ''' '''
setup.py内容
from setuptools import find_namespace_packages from setuptools import setup setup( name="webservice", packages=find_packages(), version="0.1.0", description="description", author="Author", license="License", )
执行日志对比
同时存在两个文件时的执行日志
(webservice_tool)pk@LAP1:~/webservice_tool$ pip install -e . Obtaining file:///home/pk/webservice_tool Installing build dependencies ... done Checking if build backend supports build_editable ... done Getting requirements to build editable ... done Preparing editable metadata (pyproject.toml) ... done Building wheels for collected packages: webservice Building editable for webservice (pyproject.toml) ... done Created wheel for webservice: filename=webservice-0.1.0-0.editable-py3-none-any.whl size=4070 sha256=dcb7c034ba437503d1059fe9370ccafbda144cd19f3e5d92340a63a7da396422 Stored in directory: /tmp/pip-ephem-wheel-cache-6iqiqbob/wheels/e6/b5/ba/40d8c3d66df94ee2ae46e181034e0c3c47132784db53284d0b Successfully built webservice Installing collected packages: webservice Successfully installed webservice-0.1.0
删除pyproject.toml后的执行日志
(webservice_tool) pk@LAP1:~/webservice_tool$ pip install -e . Obtaining file:///home/pk/webservice_tool Preparing metadata (setup.py) ... done Installing collected packages: webservice Attempting uninstall: webservice Found existing installation: webservice 0.1.0 Uninstalling webservice-0.1.0: Successfully uninstalled webservice-0.1.0 Running setup.py develop for webservice Successfully installed webservice-0.1.0
环境相关包版本
packaging 21.3 pyhd3eb1b0_0 pip 22.1.2 py38h06a4308_0 python 3.8.13 h12debd9_0 setuptools 61.2.0 py38h06a4308_0
项目目录结构
|-- data_utils | |-- clean_cache.py | `-- advanced_utils.py |-- deployment | |-- base_deployment | | |-- auth-proxy.yaml | | |-- kustomization.yaml | | |-- webapi.yaml | | `-- webui.yaml | `-- mysql_from_helm | |-- mysql-from-helm.yaml | `-- mysql-kustomization.yaml |-- docker-compose.yml |-- Dockerfile |-- environment.yml |-- live_api | |-- definitions.json | |-- __init__.py | `-- live_api.py |-- params.py |-- pyproject.toml |-- setup.py |-- shared_helpers | |-- data_cleaning.py | |-- handle_time.py | |-- __init__.py | |-- plot_timesequence.py | |-- read_samples.py | `-- save_samples.py |-- setup.py |-- util.py |-- webtool | |-- clean_data | | |-- clean_data.py | | `-- __init__.py | |-- evaluation | | |-- draw_figures.py | | |-- __init__.py | | `-- webtool_metrics.py | |-- __init__.py | |-- preprocess | | |-- __init__.py | | `-- preprocess.py | |-- ui | | |-- __init__.py | | `-- create_ui.py | `-- util | |-- data_input.py | |-- data_redefinitions.py | `-- __init__.py |-- webtool.egg-info | |-- dependency_links.txt | |-- entry_points.txt | |-- PKG-INFO | |-- SOURCES.txt | `-- top_level.txt `-- webtool_tests |-- clean_data | `-- test_clean_data.py |-- evaluation | `-- test_draw_figures.py |-- preprocess | `-- test_preprocess.py `-- util |-- test_data_input.py `-- test_data_redefinitions.py
解决方案
问题根源在于:当pyproject.toml存在但未指定构建后端时,pip会默认启用PEP 517/518的隔离构建流程,而非传统的setup.py develop模式。要保留pyproject.toml同时实现开发版安装,只需在pyproject.toml中添加build-system配置,明确指定使用setuptools构建后端:
修改后的pyproject.toml如下:
# Source https://github.com/psf/black#configuration-format [tool.black] line-length = 100 target-version = ['py38'] exclude = ''' ''' [build-system] requires = ["setuptools>=61.0", "wheel"] build-backend = "setuptools.build_meta"
添加该配置后,再次执行pip install -e .,就会触发与删除pyproject.toml时一致的setup.py develop流程,同时保留black的格式化配置。
备选方案(不推荐)
如果临时需要绕过隔离构建,可以执行:
pip install -e . --no-build-isolation
但此方法可能引发依赖冲突,仅建议临时测试使用。
内容的提问来源于stack exchange,提问作者PetKie
相关产品推荐
相关产品推荐

