How can I specify dependencies on operating system in Python Poetry?

6.1k views Asked by At

If I want to develop a Python package which works only in Linux and macOS. How do I specify this restriction in Python Poetry?

2

There are 2 answers

3
Sujal Singh On

In the documentation here, they mention environment markers are supported, you could use the sys_platform marker.

0
Jonathan Feenstra On

Trove classifiers in the pyproject.toml file can be used to specify which operating systems are supported. For Linux and MacOS this would be:

[tool.poetry]
classifiers = [
    "Operating System :: MacOS",
    "Operating System :: POSIX :: Linux"
]

This will however not prevent poetry from attempting to install the package on other platforms when the poetry install command is used. Support for platform-specific wheel tags has been suggested in GitHub issue #2051, which is on the to do list for poetry's 1.2 release at the time of writing.

To specify which platforms to install the package on as a dependency of another poetry project, environment markers can be used:

[tool.poetry.dependencies]
yourpackage = {version = "*", markers = "sys_platform == 'linux' or sys_platform == 'darwin'"}

Poetry will then ignore yourpackage when poetry install is used on other platforms, but not give any errors. If it is a hard dependency, it would therefore be better to indicate elsewhere which platforms are supported.