pythonsetuptoolspython-polarspython-packagingpython-wheel

Build a wheel and Install package version depending on OS


I have several python packages that need to be installed on various os/environments. These packages have dependencies and some of them like Polars needs a different package depending on the OS, for example: polars-lts-cpu on MacOS (Darwin) and polars on all the other OS.

I use setuptools to create a whl file, but the dependencies installed depend on the OS where the wheel file was created. Here is my code:

import platform
from setuptools import find_packages, setup

setup(
    ...
    install_requires=["glob2>=0.7",
                       "numpy>=1.26.4",
                       "polars>=1.12.0" if platform.system() != "Darwin" else "polars-lts-cpu>=1.12.0"]
    ...)

As mentioned above, this code installs the version of Polars according to the OS where the wheel file was created, not according to where the package will be installed.

How can I fix this?


Solution

  • Use declarative environment markers as described in PEP 496 and PEP 508:

       install_requires=[
          "polars>=1.12.0; platform_system!='Darwin'",
          "polars-lts-cpu>=1.12.0; platform_system=='Darwin'",
       ]