Advantages of Pipfile.lock Over requirements.txt

What is requirements.txt

The requirements.txt file is the way python developers have specified their dependencies for a long time. It allows you to specify depndencies by package name and version in an easy to read text file, like so:


Problems with requiremens.txt

For some developers, the format of requirements.txt fails to meet several needs.

In particular, it does not specify a hash for the installed package.

This means that if a malicious user got access to the requests pypa account, they could re-publish version 2.23.0 and pip would happily install it into your or another developer’s system as soon as they ran pip install -r requirements.txt.

Obviously this is a security vulnerability we would like to avoid.

The solution: lock files

Lock files allow us to create deterministic python builds. Similar to requirements.txt we can specify a version or version range for a given package, but lock files also include a hash of the installed package so that we know the version number we specified always corresponds to the same package code we originally installed.

There are two popular types of lock files in use right now: Pipfile.lock and poetry.lock. They are used by the pipenv and poetry libraries respectively.

I am generally a big fan of both projects. I use pipenv for my django development and poetry for package development.

Here is an example of a Pipfile.lock that describes the same packages as in our requirements.txt above:

    "_meta": {
        "hash": {
            "sha256": "acbc8c4e7f2f98f1059b2a93d581ef43f4aa0c9741e64e6253adff8e35fbd99e"
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.8"
        "sources": [
                "name": "pypi",
                "url": "",
                "verify_ssl": true
    "default": {
        "certifi": {
            "hashes": [
            "version": "==2020.4.5.1"
        "chardet": {
            "hashes": [
            "version": "==3.0.4"
        "idna": {
            "hashes": [
            "version": "==2.9"
        "requests": {
            "hashes": [
            "index": "pypi",
            "version": "==2.23.0"
        "urllib3": {
            "hashes": [
            "version": "==1.25.9"
    "develop": {}

Notice how all the same packages and package versions are present, but they now have hashes attached to them.

There are other advantages to using poetry or pipenv over plain pip, but that discussion is for another time.

Leave a Reply

Your email address will not be published. Required fields are marked *