Search code examples
python-packagingpython-poetry

Python poetry, install optional dependencies


I just want the poetry equivalent of this:

$ pip install pydantic[email]

I've read all the relevant posts. Now my pyproject.toml looks like this (I tried everything else, too):

[tool.poetry.dependencies]
pydantic = {version = "*", optional = true, extras = ["email"]}
...

[tool.poetry.extras]
email = ["pydantic"]

I also removed .venv, and poetry.lock, then $ poetry install. No use. The runtime error is clear:

ImportError: email-validator is not installed, run `pip install pydantic[email]`

I can add one detail: pydantic itself is a dependency (it was absent from pyproject.toml before I ran into this problem).


Solution

  • Add something like the following in pyproject.toml:

    [tool.poetry.dependencies]
    pydantic = {version = "*", extras = ["email"]}
    

    and then run:

    poetry install
    

    or directly via this command:

    poetry add 'pydantic[email]'