within a pyproject.toml file, using uv with setuptools as the build backend, how can I…
- User
within a pyproject.toml file, using uv with setuptools as the build backend, how can I specify a dependency which is to be installed only if the operating system is not Windows, or if it is for Linux or Linux / MacOS only?
- Assistant
In a
pyproject.tomlfile with setuptools as the build backend, you can specify platform-specific dependencies using PEP 508 environment markers. Here's how to handle each case:Not Windows
[project] dependencies = [ "some-package; platform_system != 'Windows'", # or alternatively: "some-package; sys_platform != 'win32'", ]Linux Only
[project] dependencies = [ "linux-only-package; platform_system == 'Linux'", # or alternatively: "linux-only-package; sys_platform == 'linux'", ]Linux or macOS Only
[project] dependencies = [ "unix-package; platform_system in 'Linux Darwin'", # or using sys_platform: "unix-package; sys_platform in 'linux darwin'", ]Complete Example
Here's a complete
pyproject.tomlexample:[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "myproject" version = "0.1.0" dependencies = [ "requests>=2.28.0", # installed on all platforms "pywin32; platform_system == 'Windows'", # Windows only "python-daemon; platform_system != 'Windows'", # Not on Windows "pyinotify; platform_system == 'Linux'", # Linux only "watchdog; platform_system in 'Linux Darwin'", # Linux or macOS ]Key Environment Markers
The main markers for platform detection are:
platform_system: Returns 'Windows', 'Linux', 'Darwin' (for macOS), etc.sys_platform: Returns 'win32', 'linux', 'darwin', etc.
Both work, but
platform_systemis often more readable. The environment markers are evaluated at installation time, so the appropriate dependencies will be installed based on the target platform.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.