conner.xyz
conner.xyz

Reputation: 7295

Poetry: How to publish project packages targeting multiple Python versions?

I have one project I'd like to publish as packages targeting two Python versions (3.6 and 3.8).

What I understand:

What I do not understand: how can I publish the same package for more than one Python version? I can't be the only one with this use-case right?

Edit

Doing a bit more digging, I found this https://python-poetry.org/docs/dependency-specification/#multiple-constraints-dependencies which looks like it might be relevant.

Here's the example at the link above.

[tool.poetry.dependencies]
foo = [
    {version = "<=1.9", python = "^2.7"},
    {version = "^2.0", python = "^3.4"}
]

I've also found you can specify the Python version using poetry add like this...

poetry add cleo --python 3.6.10

Which adds dependencies in pyproject.toml like this...

cleo = {version = "^0.8.1", python = "3.6.10"}

Going to experiment and see if this works.

Upvotes: 8

Views: 10030

Answers (2)

conner.xyz
conner.xyz

Reputation: 7295

No. You don't need to create multiple pyproject.toml files or otherwise create separate workflows for each Python version you're targeting (for this specific situation targeting similar versions at least).

You can simply use dependency syntax to say you want to target >=3.6<4.0 like this...

[tool.poetry.dependencies]
python = '^3.6'

And then add dependencies similarly...

poetry add <dependency> python ^3.6

Which results in something like this...

[tool.poetry.dependencies]
python = '^3.6'
cleo = {version = "^0.8.1", python = "^3.6"}
pyyaml = {version = "^5.4.1", python = "^3.6"}
...

This worked, though I further went and made some of dependencies less specific to avoid incompatibilities on certain hosts.

pyyaml = {version = "^5.0", python = "^3.6"}
...

Upvotes: 3

sinoroc
sinoroc

Reputation: 22438

You probably need something like that in your pyproject.toml:

[tool.poetry.dependencies]
python = '3.6 || 3.8'

But I am not sure on the exact notation, it's a bit vague.

It seems to generate a setup.py with the following:

'>=3.6, !=2.7.*, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, !=3.7.*'

So that would include 3.9, 3.10, etc. and this is incorrect.

Upvotes: 0

Related Questions