Environment Markers (Platform-Specific Dependencies)
Convert requirements.txt entries with PEP 508 environment markers like python_version and sys_platform to pyproject.toml. Handle platform-specific dependencies correctly.
Detailed Explanation
Environment markers (PEP 508) allow you to conditionally include dependencies based on the target environment. Common markers include python_version, sys_platform, os_name, and implementation_name.
requirements.txt with markers:
gunicorn>=21.2 ; sys_platform != "win32"
uvloop>=0.19 ; sys_platform != "win32"
winloop>=0.1 ; sys_platform == "win32"
dataclasses>=0.6 ; python_version < "3.7"
typing-extensions>=4.8 ; python_version < "3.12"
tomli>=2.0 ; python_version < "3.11"
Converted to pyproject.toml:
[project]
dependencies = [
"gunicorn>=21.2 ; sys_platform != 'win32'",
"uvloop>=0.19 ; sys_platform != 'win32'",
"winloop>=0.1 ; sys_platform == 'win32'",
"dataclasses>=0.6 ; python_version < '3.7'",
"typing-extensions>=4.8 ; python_version < '3.12'",
"tomli>=2.0 ; python_version < '3.11'",
]
Available environment markers:
| Marker | Description | Example Values |
|---|---|---|
python_version |
Python major.minor | "3.11", "3.12" |
python_full_version |
Full Python version | "3.11.7" |
sys_platform |
OS platform string | "linux", "win32", "darwin" |
os_name |
OS name | "posix", "nt" |
platform_machine |
CPU architecture | "x86_64", "aarch64" |
implementation_name |
Python implementation | "cpython", "pypy" |
Common patterns:
- Cross-platform servers: Use
gunicornon Linux/macOS,waitresson Windows - Backport packages: Include
tomlionly for Python < 3.11 (whentomllibwas added to stdlib) - Performance packages: Install
uvlooponly on non-Windows platforms
Markers are preserved verbatim during conversion. The only difference is quoting style: requirements.txt uses double quotes inside markers, while pyproject.toml strings (already in double quotes) typically use single quotes inside markers.
Use Case
Converting a cross-platform CLI tool's requirements that include platform-specific dependencies for Windows, Linux, and macOS, along with Python version backport packages.