简体   繁体   English

Django:Requirements.txt

[英]Django: requirements.txt

So far I know requirements.txt like this: Django==2.0 . 到目前为止,我知道这样的requirements.txt: Django==2.0 Now I saw this style of writing Django>=1.8,<2.1.99 现在我看到了Django>=1.8,<2.1.99

Can you explain to me what it means? 你能告诉我什么意思吗?

requirements.txt is a file where one specifies dependencies . requirements.txt是一个文件,其中指定了依赖项 For example your program will here depend on Django (well you probably do not want to implement Django yourself). 例如,您的程序在这里将取决于Django (那么您可能不想自己实现Django)。

In case one only writes a custom application, and does not plan to export it (for example as a library) to other programmers, one can pin the version of the library, for example Django==2.0.1 . 如果只编写一个自定义应用程序,并且不打算将其导出(例如作为库)导出给其他程序员,则可以固定该库的版本,例如Django==2.0.1 Then you can always assume (given pip manages to install the correct package) that your environment will ave the correct version, and thus that if you follow the correct documentation, no problems will (well should ) arise. 然后,您始终可以假设(给定的pip成功安装了正确的软件包)您的环境将具有正确的版本,因此,如果您遵循正确的文档,则不会( 应该 )出现任何问题。

If you however implement a library, for example mygreatdjangolibrary , then you probably do not want to pin the version: it would mean that everybody that wants to use your library would have to install Django==2.0.1 . 但是,如果您实现一个库,例如mygreatdjangolibrary ,那么您可能不想固定该版本:这意味着每个想要使用您的库的人都必须安装Django==2.0.1 Imagine that they want a feature that is only available in , then they can - given they follow the dependencies strictly - not do this: your library requires 2.0.1. 假设他们想要一个仅在可用的功能,那么他们可以-严格遵循依赖关系-可以不这样做:您的库需要2.0.1。 This is of course not manageable. 这当然是无法管理的。

So typically in a library, one aims to give as much freedom to a user of a library. 因此,通常在图书馆中,旨在给图书馆用户以尽可能多的自由。 It would be ideal if regardless of the Django version the user installed, your library could work. 如果用户安装了Django版本,库都可以正常工作,那将是理想的选择。

Unfortunately this would result in a lot of trouble for the library developer. 不幸的是,这将给库开发人员带来很多麻烦。 Imagine that you have to take into account that a user can use Django-1.1 up to . 想象一下,您必须考虑到用户可以使用Django-1.1到 Through the years, several features have been introduced that the library then can not use, since the programmer should be conservative, and take into account that it is possible that these features do not exist in the library the user installed. 多年来,由于程序员应该保守一些功能,因此引入了一些库无法使用的功能,并考虑到用户安装的库中可能不存在这些功能。

It becomes even worse since Django went through some refactoring: some features have later been removed, so we can not simply program on and hope that everything works out. 自从Django经过一些重构以来​​,情况变得更糟:某些功能后来被删除,因此我们不能简单地在上编程并希望一切都能解决。

So in that case, it makes sense to specify a range of versions we support. 因此,在这种情况下,指定我们支持的版本范围是有意义的。 For example we can read the documentation of , and look to the release notes to see if something relevant changed in , and let tox test both versions for the tests we write. 例如,我们可以阅读的文档,并查看发行说明,以查看是否有相关的更改,并让我们为编写的测试对这两个版本进行tox测试。 We thus then can specify a range like Django>=2.0,<2.1.99 . 因此,我们可以指定一个范围,例如Django>=2.0,<2.1.99

This is also important if you depend on several libraries that each a common requirement. 如果您依赖于每个共同要求的几个库,这也很重要。 Say for example you want to install a library liba , and a library libb , both depend on Django, bot the two have a different range, for example: 假设您要安装一个库liba和一个库libb ,它们都依赖于Django,而bot则两者的范围不同,例如:

liba:
    Django>=1.10, <2.1
libb:
    Django>=1.9, <1.11

Then this thus means that we can only install a Django version between >=1.10 and <1.11 . 那么这意味着我们只能安装>=1.10<1.11之间的Django版本。

The above even easily gets more complex. 以上甚至很容易变得更加复杂。 Since liba and libb of course have versions as well, for example: 因为libalibb当然也有版本,例如:

liba-0.1:
    Django>=1.10, <2.1
liba-0.2:
    Django>=1.11, <2.1
liba-0.3:
    Django>=1.11, <2.2

libb-0.1:
    Django>=1.5, <1.8
libb-0.2:
    Django>=1.10, <2.0

So if we now want to install any liba , and any libb , we need to find a version of liba and libb that "allows" us to install a Django version, and that is not that trivial since for example if we would pick libb-0.1 , then there is no version of liba that supports an "overlapping" Django version. 因此,如果我们现在想安装任何liba和任何libb ,我们需要找到一个“允许”我们安装Django版本的libalibb版本,但这并不是那么简单,因为例如,如果我们选择libb-0.1 ,那么就没有支持“重叠” Django版本的liba版本。

To the best of my knowledge, pip currently has no dependency resolution algorithm. 据我所知, pip目前没有依赖项解析算法。 It looks at the specification, and each time aims to pick the most recent that is satisfying the constraints, and recursively installs the dependencies of these packages. 它着眼于规范,每次旨在选择满足约束条件的最新版本,然后递归安装这些软件包的依赖项。

Therefore it is up to the user to make sure that (sub)dependencies do not conflict: if we would specify liba libb==0.1 , then pip will probably install Django-2.1 , and then find out that libb can not work with this. 因此,由用户确保(子)依赖项不冲突:如果我们将liba指定为libb==0.1 ,则pip可能会安装Django-2.1 ,然后发现libb不能与此一起使用。

There are some dependency resolution programs . 有一些依赖项解决程序 But the problem turns out to be quite hard (it is NP-hard if I recall correctly). 但是事实证明这个问题很难解决(如果我没记错的话,这是NP问题 )。 So that means that for a given dependency tree, it can takes years to find a valid configuration. 因此,这意味着对于给定的依赖关系树,可能需要数年才能找到有效的配置。

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

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