8

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

What I understand:

  • How to install and activate different python versions using pyenv.
  • How to get poetry to create virtual environments that correspond to the chosen Python version.
  • How to setup pyproject.toml to specify the python version, manage dependencies, and publish a package using this configuration.

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?

  • Does need two pyproject.toml files? (one for each python version and set of corresponding dependencies...)
  • Are there established ways of doing this with Poetry, or are other tools/workflows necessary?

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.

2
  • How does your pyproject.toml look like now? Commented Jan 28, 2021 at 22:34
  • [tool.poetry.dependencies] python = "^3.8" cleo = "^0.8.1" ... [tool.poetry.dev-dependencies] pytest = "^5.4.3" ... Commented Jan 28, 2021 at 22:42

2 Answers 2

3

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"}
...
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

python = '>=3.6, != 3.7, <=3.8' does not really seem to work either.
Hm, this behavior doesn't look correct to me. Can you please open an issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.