[Python] Choose between extras of the same dependency with Poetry

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python project managed with Poetry.

    I want to install either psycopg[binary] or psycopg[c] via

    $ poetry install -E pgbinary


    or

    $ poetry install -E pgc


    These extras should be mutually exclusive, i.e.

    $ poetry install -E pgbinary -E pgc


    should raise an error.

    This is what I wrote in the pyproject.toml:

    [tool.poetry.dependencies]
    python = "^3.9"
    ...
    psycopg = { version = "^3.1.0", extras = ["binary", "c"], optional = true}


    [tool.poetry.extras]
    pgbinary = ["psycopg[binary]"]
    pgc = ["psycopg[c]"]


    But when I do one of the following

    $ poetry install
    $ poetry install -E pgbinay
    $ poetry install -E pgc


    I always get this

    The Poetry configuration is invalid:
    - data.extras.pgbinary[0] must match pattern ^[a-zA-Z-_.0-9]+$


    The problem seems related to the square brackets in psycopg[binary] (or psycopg[c]).

    Is there a way to choose which extra to install for a specific library during the poetry install?

    UPDATE


    I've solved with this for now

    [tool.poetry.dependencies]
    ...
    psycopg = {version = "^3.1.9", optional=true}
    psycopg-binary = {version = "^3.1.9", optional=true}
    psycopg-c = {version = "^3.1.9", optional=true}

    [tool.poetry.extras]
    pgbinary = ["psycopg", "psycopg-binary"]
    pgc = ["psycopg", "psycopg-c"]


    It works, but has stated here

    You shouldn’t install this package directly: use instead

    pip install "psycopg[c]"

    to install a version of the optimization package matching the psycopg version installed.


    If there is a way to write version = "^3.1.9" just once, it would be great.

    Moreover, the extras are still not mutually exclusive.

    Continue reading...

Compartilhe esta Página