Search code examples
pythondependenciespython-poetry

How can I add implicit dependencies to a dependency in Python Poetry?


My project depends on a package that I do not maintain: PackageA. PackageA depends on PackageB but does not explicitly specify PackageB as a dependency in its setup.py file.

Ideally, the PackageA maintainer would explicitly add PackageB as a dependency, but I can't control that.

I could simply poetry add PackageB to my project but then I would be taking a dependency on PackageB, when really I only want to depend on PackageA.

I attempted to manually modify the [package.dependencies] for PackageA in the poetry.lock file to add PackageB = * but Poetry began complaining. That also seems like a bit of a hack as the dependency relationship is still not modelled anywhere in the pyproject file.

Is there a way that I can tell Poetry that I want to take a dependency on PackageA but that PackageA also has a dependency on PackageB?


Solution

  • it seems like some extra work to me but i guess you could do

    [tool.poetry.group.packageA]
    optional = true
    
    [tool.poetry.group.packageA.dependencies]
    packageA = "^1.3.4"
    packageB = "^1.2.3"
    

    then install with poetry install --with packageA

    or remove the optional=true flag that i left in like a dummy and it will work just as you expect :P I think (thanks @Nathan)

    or you could add it to the extras section

    [tool.poetry.extras]
    packageA = ["packageA","packageB"]
    

    and install with poetry install -E packageA

    but if it was me i would just add it to my main dependencies