Generate aligned requirements.txt and dev-requirements.txt with pip-compile

795 views Asked by At

I have a Python project that depends on two packages moduleA and moduleB. I have the following pyproject.toml:

[project]
name = "meta-motor"
version = "3.1.0.dev"
dependencies = [
    "moduleA==1.0.0"
]

[project.optional-dependencies]
dev = [
    "moduleB==1.0.0"
]

with moduleA depending on moduleC>=1.0.0 and moduleB depending on moduleC==1.1.0.

I compile my requirements.txt and dev-requirements.txt like this:

$ pip-compile -o requirements.txt pyproject.toml 
$ pip-compile --extra dev -o dev-requirements.txt pyproject.toml 

With this, I get

requirements.txt

moduleA==1.0.0
    # via pyproject.toml
moduleC==1.2.0
    # via moduleA

dev-requirements.txt

moduleB==1.0.0
    # via pyproject.toml
moduleA==1.0.0
    # via pyproject.toml
moduleC==1.1.0
    # via 
    #   moduleB
    #   moduleA

As you can see, the moduleC version is different in both requirements.txt files. How can I solve this so I have moduleC==1.1.0 in both ?

I could specify moduleC==1.1.0 in my pyproject.toml, but this is not practicable for larger project with lots of dependencies like this.

1

There are 1 answers

0
Albert Tugushev On

This PR #1936 introduces -c/--constraint option to pip-compile which allows you to pass requirements.txt as a constraint file when compiling dev-requirements.txt, for example:

$ pip-compile --extra dev -o dev-requirements.txt -c requirements.txt pyproject.toml 

This should resolve the issue.