何时使用 pip 需求文件与 setup.py 中的 install_requires? [英] When to use pip requirements file versus install_requires in setup.py?

查看:64
本文介绍了何时使用 pip 需求文件与 setup.py 中的 install_requires?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 pip 和 virtualenv 来打包和安装一些 Python 库.

I'm using pip with virtualenv to package and install some Python libraries.

我想我正在做的是一个非常常见的场景.我是几个可以明确指定依赖项的库的维护者.我的一些库依赖于第三方库,这些库具有我无法控制的传递依赖项.

I'd imagine what I'm doing is a pretty common scenario. I'm the maintainer on several libraries for which I can specify the dependencies explicitly. A few of my libraries are dependent on third party libraries that have transitive dependencies over which I have no control.

我想要实现的是在我的库之一上进行 pip install 以下载/安装其所有上游依赖项.我在 pip 文档中遇到的问题是 if/how requirements文件可以自己完成,或者如果它们真的只是使用install_requires的补充.

What I'm trying to achieve is for a pip install on one of my libraries to download/install all of its upstream dependencies. What I'm struggling with in the pip documentation is if/how requirements files can do this on their own or if they're really just a supplement to using install_requires.

我是否会在我的所有库中使用 install_requires 来指定依赖项和版本范围,然后只使用需求文件来解决冲突和/或冻结它们以进行生产构建?

Would I use install_requires in all of my libraries to specify dependencies and version ranges and then only use a requirements file to resolve a conflict and/or freeze them for a production build?

让我们假设我生活在一个虚构的世界中(我知道,我知道)并且我的上游依赖项很简单,并且保证永远不会冲突或破坏向后兼容性.我会被迫完全使用 pip 需求文件还是让 pip/setuptools/distribute 基于 install_requires 安装所有内容?

Let's pretend I live in an imaginary world (I know, I know) and my upstream dependencies are straightforward and guaranteed to never conflict or break backward compatibility. Would I be compelled to use a pip requirements file at all or just let pip/setuptools/distribute install everything based on install_requires?

这里有很多类似的问题,但我找不到像何时使用一个或另一个或将它们和谐地一起使用那样基本的问题.

There are a lot of similar questions on here, but I couldn't find any that were as basic as when to use one or the other or using them both together harmoniously.

推荐答案

我的理念是 install_requires 应该指出您需要的最低限度.如果您知道某些版本将无法运行,则它可能包括版本要求;但它不应该有您不确定的版本要求(例如,您不确定某个依赖项的未来版本是否会破坏您的库).

My philosophy is that install_requires should indicate a minimum of what you need. It might include version requirements if you know that some versions will not work; but it shouldn't have version requirements where you aren't sure (e.g., you aren't sure if a future release of a dependency will break your library or not).

另一方面,需求文件应该表明您知道什么可以工作,并且可能包含您推荐的可选依赖项.例如,您可能使用 SQLAlchemy 但建议使用 MySQL,因此将 MySQLdb 放在需求文件中).

Requirements files on the other hand should indicate what you know does work, and may include optional dependencies that you recommend. For example you might use SQLAlchemy but suggest MySQL, and so put MySQLdb in the requirements file).

所以,总而言之:install_requires 是让人们远离你知道行不通的事情,而需求文件引导人们走向你知道行得通的事情.其原因之一是 install_requires 要求总是被检查,并且不能在不实际更改包元数据的情况下禁用.所以你不能轻易尝试新的组合.仅在安装时检查需求文件.

So, in summary: install_requires is to keep people away from things that you know don't work, while requirements files to lead people towards things you know do work. One reason for this is that install_requires requirements are always checked, and cannot be disabled without actually changing the package metadata. So you can't easily try a new combination. Requirements files are only checked at install time.

这篇关于何时使用 pip 需求文件与 setup.py 中的 install_requires?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆