Search code examples
pythonpython-poetry

Python Poetry specify dependency on `sentry-sdk[flask]`


I'm trying to add support for Sentry to an existing Python Flask application that uses Python Poetry for dependency management.

The Sentry docs say to run this:

pip install --upgrade 'sentry-sdk[flask]

That works. But I want to convert that into a pyproject.toml dependency entry for Python Poetry. If I try just this:

[tool.poetry.dependencies]
# <snip>
sentry-sdk = "1.5.12"

I get a SolverProblemError:

... depends on sentry-sdk (1.5.12) which doesn't match any versions, version solving failed.

If I try:

[tool.poetry.dependencies]
# <snip>
sentry-sdk[flask] = "1.5.12"

I get Invalid TOML file.

How do I convert this pip dependency to Python pyproject.toml format?


Solution

  • This is how you can specify a dependency with extras:

    [tool.poetry.dependencies]
    python = "^3.9"
    sentry-sdk = {extras = ["flask"], version = "1.5.12"}
    

    See the following section of the documentation: Dependency extras

    You can also achieve this by running:

    poetry add sentry-sdk[flask]==1.5.12