Search code examples
pythondependency-managementrequirements.txt

Ensure that Python CLI matches version set in requirements.txt


I'm co-developing a CLI for https://pretextbook.org/ authors. When an author runs pretext new we generate boilerplate files for their new project, including a requirements.txt that matches the current version of the CLI. If the author updates their CLI package, their project's source files may no longer be compatible with the updated package. Is there a canonical way to parse a requirements.txt file and print out a warning that the CLI version being used does not match the version set in requirements.txt?


Solution

  • If I wanted to parse requirements file like pip, I'd use pip-requirements-parser, since pip itself can't easily be used as a library, and pip-requirements-parser claims to be as close to pip as possible.

    On another hand, I don't think you need that at all. If user is expected to edit requirements file, then you can't really deduce anything from these versions.

    Consider this:

    1. New project is created using pretext v1.2.3
    2. requirements.txt contains pretext==1.2.3
    3. User edits the file to make it pretext>=1.2.3
    4. User updates pretext to v2.3.4
    5. Project source is no longer compatible with pretext, but CLI version matches requirements.txt specification.

    Hence you need another solution, like a dedicated .pretext_project_version file, so you can reasonably expect user to not confuse project requirements and compatible CLI version. This would also simplify project versioning for yourself, because you'll have to bump that version only on incompatible changes, rather than encode logic like "project v 1.0 is compatible with 1.5 and 2.0, but not 2.2"