如何在 pyproject.toml 中使用 --no-deps?

问题描述 投票:0回答:1

我有一个

pyproject.toml
文件,其中包含一些 python 包。

它有这样一个部分:

dependencies = [
    'pandas == 1.4.3',
    'streamlit == 1.11.1',
    ...
]

然后我通常使用

安装这些依赖项

python -m pip install .[dependencies]

我需要安装一个新软件包,该软件包的依赖项之一存在冲突。我查了一下,“冲突”是肤浅的 - 即。新包的维护者只是“忘记”更新requirements.txt以允许pandas>=1.4。

我可以通过运行来做到这一点

python -m pip install {new_package} --no-deps

但是有没有办法在

pyproject.toml
文件中做到这一点?

python pip dependency-management pyproject.toml
1个回答
0
投票

是的,你可以!

通过利用
pip
的直接引用
以及 --no-deps 标志,直接在
pyproject.toml
内部。

您的依赖项条目应类似于:

dependencies = [
    'pandas == 1.4.3',
    'streamlit == 1.11.1',
    '{{NEW_PACKAGE}} @ git+https://github.com/{{USER}}/{{NEW_PACKAGE}}.git#egg={{NEW_PACKAGE}} --no-deps',
]

这有点 hacky,因为它避免了 PyPi。

一个更强大的解决方案(尽管涉及更多)是使用 可选依赖项

单独定义有问题的部门
[project]
dependencies = [
    'pandas == 1.4.3',
    'streamlit == 1.11.1',
]

[project.optional-dependencies]
main = [
    'pandas == 1.4.3',
    'streamlit == 1.11.1',
]

problematic = [
    'new-package'
]

然后分别安装它们:

> python -m pip install .[main]
> python -m pip install .[problematic] --no-deps

为了方便起见,您可以将两个安装命令包装在自定义 bash/ps1 脚本中:

重击

python -m pip install .[main] && python -m pip install .[problematic] --no-deps

Powershell

python -m pip install .[main]; python -m pip install .[problematic] --no-deps
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.