在安装pip需求时忽略一些需求

2024-10-01 13:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用requirements.txt为我的virtualenv安装需求。我将ansible用于在远程主机上安装需求的部署。在

问题:

  1. 忽略某些要求

  2. 忽略已经安装的需求(比如pip freeze,如果包出现,就不要安装它,甚至不要升级)

我的解决方案:

  1. 我可以grep已安装的包并使requirements2.txt只包含所需的包。(同时,从GIT中删除正在安装的包)

  2. 我不明白在这种情况下--ignore-installed会做什么?

  3. 还有其他解决办法吗?


Tags: pipgittxt远程virtualenv部署情况ansible
1条回答
网友
1楼 · 发布于 2024-10-01 13:36:05

对于选择性依赖项安装,唯一的方法确实是根据您的条件对requirements.txt文件进行grep/过滤。但是,几乎没有现成的解决方案可用:


如果您有一个virtualenv,只需要将其快速升级到新的要求或版本限制,但如果现有的软件包满足条件,则不需要升级,则可以使用

pip install -U  upgrade-strategy=only-if-needed  ...

手册上说:

upgrade-strategy <upgrade_strategy> Determines how dependency upgrading should be handled. "eager" - dependencies are upgraded regardless of whether the currently installed version satisfies the requirements of the upgraded package(s). "only-if-needed" - are upgraded only when they do not satisfy the requirements of the upgraded package(s).


对于可选依赖项,典型的解决方案是setuptools的extra requirements。例如,我将其用于开发和文档构建需求:

^{pr2}$

然后您可以按如下方式安装它,既可以从PyPI/DevPI repos,也可以在本地(作为一个可编辑的库):

pip install mylib[dev]
pip install mylib[doc]
pip install -e .[doc,dev]

您可以使用可选依赖项为“额外模式”定义任何名称。在

相关问题 更多 >