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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41678951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:37:37  来源:igfitidea点击:

Ignoring some requirements when installing pip requirements

pythonbashpiprequirements.txt

提问by Ranvijay Jamwal

I am using requirements.txtto install requirements for my virtualenv. I use ansible for deployments which installs requirements on remote hosts.

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

Problem:

问题:

  1. Ignoring some requirements

  2. Ignoring already installed requirements (something like pip freezeand if the package shows up, don't install it and don't even upgrade)

  1. 忽略一些要求

  2. 忽略已安装的要求(例如pip freeze,如果软件包出现,请不要安装它,甚至不要升级)

Solutions according to me:

根据我的解决方案:

  1. I can grep installed packages and make a requirements2.txthaving only the ones needed. (Also, remove packages being installed from GIT)

  2. I don't understand what --ignore-installedwould do in this case?

  3. Any other solution?

  1. 我可以使用 grep 安装的软件包并制作requirements2.txt只需要的软件包。(另外,从 GIT 中删除正在安装的包)

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

  3. 还有其他解决方案吗?

回答by Sergey Vasilyev

For selective dependencies installing, the only way is indeed to grep/filter the requirements.txtfile according to your criteria. However, there are few ready solutions that might be of use:

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



If you have a virtualenv and only need to quickly upgrade it to the new requirements or version restrictions, but do not upgrade if the existing packages meet the criteria, you can use

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

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

As the manual says:

正如手册所说:

--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).

--upgrade-strategy <upgrade_strategy>确定应如何处理依赖项升级。“eager” - 无论当前安装的版本是否满足升级包的要求,依赖项都会升级。“only-if-needed” - 仅当它们不满足升级包的要求时才升级。



For the optional dependencies, the typical solution is the setuptools' extra requirements. For example, I use it for the development & doc-building requirements:

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

# setup.py
setup(
    ...,
    extras_require={
        'dev': ["pdbpp", "ipython"],
        'doc': ["sphinx"],
    },
)

Then you can install it as follows, both from the PyPI/DevPI repos, and locally (as an editable library):

然后您可以按如下方式安装它,从 PyPI/DevPI 存储库和本地(作为可编辑库):

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

You can define any names for the "extra modes" with optional dependencies.

您可以为具有可选依赖项的“额外模式”定义任何名称。