何时使用 pip 需求文件与 setup.py 中的 install_requires ?
我使用 pip 和 virtualenv 来打包和安装一些 Python 库。
我想我正在做的事情是一个非常常见的场景。我是几个库的维护者,我可以明确指定这些库的依赖关系。我的一些库依赖于第三方库,这些库具有我无法控制的传递依赖项。
我想要实现的是在我的一个库上进行 pip install
以下载/安装其所有上游依赖项。我在 pip 文档中遇到的问题是 if/how 要求文件可以单独执行此操作,或者如果它们实际上只是使用install_requires
的补充。
我是否会在所有库中使用 install_requires 来指定依赖项和版本范围,然后仅使用需求文件来解决冲突和/或冻结它们以进行生产构建?
让我们假设我生活在一个想象的世界中(我知道,我知道),我的上游依赖关系很简单,并且保证永远不会冲突或破坏向后兼容性。我是否必须使用 pip 要求文件,或者只是让 pip/setuptools/distribute 根据 install_requires
安装所有内容?
这里有很多类似的问题,但我找不到任何像何时使用其中之一或和谐地使用它们一样基本的问题。
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.
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
.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的理念是
install_requires
应该指出您需要的最低限度。如果您知道某些版本无法运行,它可能会包含版本要求;但它不应该有您不确定的版本要求(例如,您不确定依赖项的未来版本是否会破坏您的库)。另一方面,需求文件应该表明您知道什么可以工作,并且可能包括您推荐的可选依赖项。例如,您可能使用 SQLAlchemy 但建议使用 MySQL,因此将 MySQLdb 放在需求文件中)。
因此,总而言之:
install_requires
是为了让人们远离那些你知道不起作用的东西,而需求文件则是为了引导人们走向你知道起作用的东西。原因之一是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).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).
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 thatinstall_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.这是我在 setup.py 中放入的内容:
here's what I put in my setup.py:
Python 打包用户指南有一个关于此主题的页面,我强烈建议您阅读它:
摘要:
install_requires
用于列出包的依赖项,必须安装这些依赖项才能使包正常工作。它并不意味着将依赖项固定到特定版本,但接受范围,例如install_requires=['django>=1.8']
。install_requires
由pip install name-on-pypi
和其他工具观察。requirements.txt
只是一个文本文件,您可以选择对其运行pip install -rrequirements.txt
。它意味着固定所有依赖项和子依赖项的版本,如下所示:django==1.8.1
。您可以使用pip freeze > 创建一个需求.txt
。 (某些服务,例如 Heroku,会自动为您运行pip install -rrequirements.txt
。)pip install name-on-pypi
不会查看requirements。 txt
,仅在install_requires
处。The Python Packaging User Guide has a page about this topic, I highly recommend you read it:
Summary:
install_requires
is there to list the dependencies of the package that absolutely must be installed for the package to work. It is not meant to pin the dependencies to specific versions, but ranges are accepted, for exampleinstall_requires=['django>=1.8']
.install_requires
is observed bypip install name-on-pypi
and other tools.requirements.txt
is just a text file, that you can choose to runpip install -r requirements.txt
against. It's meant to have versions of all dependencies and subdependencies pinned, like this:django==1.8.1
. You can create one usingpip freeze > requirements.txt
. (Some services, like Heroku, automatically runpip install -r requirements.txt
for you.)pip install name-on-pypi
does not look atrequirements.txt
, only atinstall_requires
.我只使用
setup.py
和install_requires
因为只有一个地方可以查看。它与拥有需求文件一样强大,并且无需重复维护。I only ever use a
setup.py
andinstall_requires
because there is only one place to look at. It is just as powerful as having a requirements file and there is no duplication to maintain.