DEVELOPMENT | PYTHON | DATA SCIENCE

A simple concept with a number of tangible benefits

Courtesy of imgflip: https://imgflip.com/i/5mkvxf

Just about any Python developer from beginner to expert has heard about the requirements.txt file. It documents our package dependencies for Python, and it can be version controlled along with our code.

An example requirements.txt file might look something like this:

coverage==5.5
fastapi==0.68.1
pydantic==1.8.2
pytest==6.2.3
regex==2021.4.4
requests==2.25.1
[ . . . ]

However, I’d like to introduce a simple but effective concept for long-term maintenance of Python repositories — the requirements-core.txt file.

Most Python developers only intend to use a handful of packages in their project, and the rest of the packages listed in requirements.txt are sub-dependencies. Personally, I haven’t heard of at least half of the packages that show up in a given requirements.txt file that I’ve generated.

Additionally, most Python developers don’t care about the version of a package they’re using unless:

  • The package is out of date
  • The package has a version conflict with another package
  • Certain feature(s) are only available in specific version(s) of a package

We’ll refer to the full, explicit list of dependencies that our project needs in the absolute requirements. We’ll refer to the packages that we’re truly cognizant of the core requirements.

The requirements-core.txt file is simply a list of package dependencies that is stripped down to the core requirements of a project, minus any version number hints.

Let’s imagine these three files exist in our project…

requirements-core.txt

coverage
fastapi
pytest
regex
requests

requirements.txt

attrs==21.2.0
certifi==2021.5.30
charset-normalizer==2.0.4
coverage==5.5
fastapi==0.68.1
idna==3.2
iniconfig==1.1.1
packaging==21.0
pip==20.2.3
pluggy==1.0.0
py==1.10.0
pydantic==1.8.2
pyparsing==2.4.7
pytest==6.2.5
regex==2021.8.28
requests==2.26.0
setuptools==49.2.1
starlette==0.14.2
toml==0.10.2
typing-extensions==3.10.0.2
urllib3==1.26.6

requirements-update.sh

#!/bin/bashpip install -r requirements-core.txt
pip freeze > requirements.txt

As demonstrated above, a project with 5 core requirements (coverage, fastapi, pytest, regex, and requests) ultimately had 21 absolute requirements.

Six months (or multiple years) down the road, most or all of the maintainers of this project would have forgotten what the core requirements were if they weren’t documented elsewhere.

By extracting the core requirements from our absolute…

Continue reading: https://towardsdatascience.com/why-you-should-consider-adding-requirements-core-txt-to-your-next-python-project-2c36c1381b12?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com