Sarah Messer
Sarah Messer

Reputation: 4083

How do I specify "extra" / bracket dependencies in a pyproject.toml?

I'm working on a project that specifies its dependencies using Poetry and a pyproject.toml file to manage dependencies. The documentation for one of the libraries I need suggests pip-installing with an "extra" option to one of the dependencies, like this:

pip install google-cloud-bigquery[opentelemetry]

How should I reflect this requirement in the pyproject.toml file? Currently, there are a few lines like this:

[tool.poetry.dependencies]
python = "3.7.10"
apache-beam = "2.31.0"
dynaconf = "3.1.4"
google-cloud-bigquery = "2.20.0"

Changing the last line to

google-cloud-bigquery[opentelemetry] = ">=2.20.0"

yields

Invalid TOML file /home/jupyter/vertex-monitoring/pyproject.toml: Unexpected character: 'o' at line 17 col 22

Other variants that don't seem to be parsed properly:

google-cloud-bigquery["opentelemetry"] = "2.20.0"

There are other StackOverflow questions which look related, as well as several different PEP docs, but my searches are complicated because I'm not sure whether these are "options" or "extras" or something else.

Upvotes: 35

Views: 17353

Answers (2)

finswimmer
finswimmer

Reputation: 15291

You can add it by poetry add "google-cloud-bigquery[opentelemetry]". This will result in:

[tool.poetry.dependencies]
...
google-cloud-bigquery = {extras = ["opentelemetry"], version = "^2.34.2"}

Upvotes: 46

ti7
ti7

Reputation: 18930

Though the syntax looks a little strange, TOML supports quoting keys to escape special characters as of some version "Less restrictive bare keys"
https://github.com/toml-lang/toml/pull/283

"google-cloud-bigquery[opentelemetry]"

This syntax may work for you!

[tool.poetry.dependencies]
python = "3.7.10"
apache-beam = "2.31.0"
dynaconf = "3.1.4"
"google-cloud-bigquery[opentelemetry]" = ">=2.20.0"

TOML may expect ^ >=, though the syntax isn't clear from the docs

Upvotes: -2

Related Questions