简体   繁体   English

何时在setup.py中使用pip requirements文件与install_requires?

[英]When to use pip requirements file versus install_requires in setup.py?

I'm using pip with virtualenv to package and install some Python libraries. 我正在使用带有virtualenv的pip来打包和安装一些Python库。

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. 我想要实现的是在我的一个库上pip install来下载/安装它的所有上游依赖项。 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 . 我在pip文档中遇到的问题是, 需求文件是否/如何自行完成,或者它们实际上只是对使用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? 我是否会在所有库中使用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 ? 我是否会被迫使用pip需求文件或者只是让pip / setuptools / distribute根据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. 这里有很多类似的问题,但我找不到任何与使用其中一个或者和谐地使用它们一样基本的问题。

My philosophy is that install_requires should indicate a minimum of what you need. 我的理念是install_requires应该指出你需要的最小值。 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 (eg, 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). 例如,您可以使用SQLAlchemy但建议MySQL,因此将MySQLdb放在需求文件中)。

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. 总而言之: install_requires是让人们远离那些你知道不起作用的东西,而需求文件引导人们走向你认识的事情。 One reason for this is that install_requires requirements are always checked, and cannot be disabled without actually changing the package metadata. 其中一个原因是始终检查install_requires要求,并且在未实际更改包元数据的情况下无法禁用。 So you can't easily try a new combination. 所以你不能轻易尝试新的组合。 Requirements files are only checked at install time. 只在安装时检查需求文件。

here's what I put in my setup.py: 这是我在setup.py中添加的内容:

# this grabs the requirements from requirements.txt
REQUIREMENTS = [i.strip() for i in open("requirements.txt").readlines()]

setup(
    .....
    install_requires=REQUIREMENTS
)

The Python Packaging User Guide has a page about this topic, I highly recommend you read it: “Python包装用户指南”有一个关于此主题的页面,我强烈建议您阅读它:

Summary: 摘要:

install_requires is there to list the dependencies of the package that absolutely must be installed for the package to work. install_requires用于列出必须安装的软件包的依赖项才能使软件包正常工作。 It is not meant to pin the dependencies to specific versions, but ranges are accepted, for example install_requires=['django>=1.8'] . 它并不是要将依赖项固定到特定版本,而是接受范围,例如install_requires=['django>=1.8'] install_requires is observed by pip install name-on-pypi and other tools. pip install name-on-pypi和其他工具可以观察到install_requires

requirements.txt is just a text file, that you can choose to run pip install -r requirements.txt against. requirements.txt只是一个文本文件,您可以选择运行pip install -r requirements.txt It's meant to have versions of all dependencies and subdependencies pinned, like this: django==1.8.1 . 它的意思是将所有依赖项和子依赖项的版本固定,如下所示: django==1.8.1 You can create one using pip freeze > requirements.txt . 您可以使用pip freeze > requirements.txt创建一个。 (Some services, like Heroku, automatically run pip install -r requirements.txt for you.) pip install name-on-pypi does not look at requirements.txt , only at install_requires . (有些服务,比如Heroku,会为你自动运行pip install -r requirements.txt 。) pip install name-on-pypi不会查看requirements.txt ,只能在install_requires查看。

I only ever use a setup.py and install_requires because there is only one place to look at. 我只使用setup.pyinstall_requires因为只有一个地方可以查看。 It is just as powerful as having a requirements file and there is no duplication to maintain. 它与拥有需求文件一样强大,并且没有重复维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM