17

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.

Sarah Messer
  • 3,592
  • 1
  • 26
  • 43
  • 1
    try quoting the left-side string too – ti7 Mar 07 '22 at 20:17
  • @t17 Ah! `"google-cloud-bigquery[opentelemetry]" = "2.20.0"` appears to parse at least. Thank you! Although it still tells me `google-cloud-bigquery[opentelemetry] (>=2.0.0) which doesn't match any versions`, so maybe not working right either – Sarah Messer Mar 07 '22 at 20:26

2 Answers2

26

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"}
finswimmer
  • 10,896
  • 3
  • 34
  • 44
  • ahah - this looks like what's wanted if it works! – ti7 Mar 07 '22 at 20:57
  • Confirmed in Poetry v1.3.2: `poetry add "aiohttp[speedups]"` resulted in pyproject.toml entry: `aiohttp = {extras = ["speedups"], version = "^3.8.3"}` and successfully installed dependencies. – Liquidgenius Feb 06 '23 at 14:22
-3

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

ti7
  • 16,375
  • 6
  • 40
  • 68
  • When I tried this, it seemed to try to install a version of a package with the literal name "google-cloud-bigquery[opentelemetry]", not package "google-cloud-bigquery" with option "opentelemetry" – Sarah Messer Mar 07 '22 at 21:50
  • 1
    alas, I've seen something similar, but never used Poetry - glad the other Answer works! – ti7 Mar 07 '22 at 21:56