为什么 pipenv 需要 Pipfile 和 Pipfile.lock?

Why does pipenv need Pipfile and Pipfile.lock?

(我认为[!])我了解pipenv(以及其他venvs)背后的原理并经常使用它们。但是,我从来没有真正理解为什么 pipenv 需要 PipfilePipfile.lock 文件。

This answer implies, and this tutorial

Now, once you get your code and Pipfile.lock in your production environment, you should install the last successful environment recorded:

$ pipenv install --ignore-pipfile

但它并没有解释为什么 Pipfile.lock 需要被使用。即 .lock 文件包含哪些 Pipfile 不包含的内容,以及为什么 Pipfile 足以与其他开发人员共享:

Now let’s say another developer wants to make some additions to your code. In this situation, they would get the code, including the Pipfile, and use this command:

$ pipenv install --dev

但还不足以用于在生产环境中复制您的环境吗?

官方Pipfile项目has something to say about this:

The concrete requirements for a Python Application would come from Pipfile. This would include where the packages should be fetched from and their loose version constraints.

The details of the environment (all installed packages with pinned versions and other details) would be stored in Pipfile.lock, for reproducibility. This file will be automatically generated and should not be modified by the user.

换句话说,Pipfile适用于Pipfile.lock适用于计算机

在您的 Pipfile 中,您在该文件中列出了您想要的内容,并以一种有点松散的方式定义它们,例如“Django 版本 2 或更高版本”。但这还不足以确定性地重现环境。那是指“Django 2.0.3”还是“Django 2.1.0”?

Pipfile.lock 准确地指定了需求,它也准确地指定了 dependencies。例如,如果您明确想要 foo 并将其放入您的 Pipfile,您的 Pipfile.lock 将生成并将其锁定到特定版本。如果foo本身依赖于bar,而bar依赖于quuxflorp,则Pipfile.lock文件将锁定barquuxflorp 也向下,因此依赖关系中的微小差异不会破坏事物。

正如@Chris 所说,Pipfile.lock 用于计算机,而 Pipfile 用于人类。如果你看一下Pipfile.lock文件,你会发现每个依赖甚至都有sha256代码!

那个文件是人类无法处理的,你只能处理Pipfile。但是 Pipfile 不够严格,无法重现完全相同的环境。所以这就是为什么我们还需要 Pipfile.lock.

这是一个类似于 npm 的工具。(也许吧?)
Pipfile是识别你项目的依赖关系,你会从Pipfile.
得到依赖树 但是根据不同的来源,你会得到不同的包。因此,您可以从 .lock 文件中获取实际的本地依赖项。
例如,在 Pipfile 中,您可以看到类似这样的内容:

matplotlib = "*"
numpy = "*"

但是在 .lock 文件中,您会看到实际的依赖关系,如:

"pytz": {
        "hashes": [
            "sha256:a061aa0a9e06881eb8b3b2b43f05b9439d6583c206d0a6c340ff72a7b6669053",
            "sha256:ffb9ef1de172603304d9d2819af6f5ece76f2e85ec10692a524dd876e72bf277"
        ],
        "version": "==2018.5"
    }

总之,Pipfile是为了更兼容,但是.lock文件是为了获取本地的实际依赖。