'Install python dependency only when platform is Windows

I want to add pywin32 as Conditional Python Dependencies to setup.py when platform_system == Windows

Could anyone give me a hint on how to make it work?

After exploring the stackoverflow, haven't found a answer to python2.7.

I'm using Python 2.7, setuptools 28.x.x, pip 19.x.x. Egg-info is auto-build.

from setuptools import setup, find_packages
import platform

platform_system = platform.system()

setup(
    name=xxx,
    version=xxx,
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'matplotlib',
    ],
    extras_require={
        'platform_system=="Windows"': [
            'pywin32'
        ]
    },
    entry_points='''
        [console_scripts]
        xx:xx
    ''',
)

I don't understand how the keys in extras_require work. Would platform_system refer to the definition of platform_system in the front?

I also tried:

from setuptools import setup, find_packages
import platform

setup(
    xxx
    install_requires=[
        'matplotlib',
        'pywin32;platform_system=="Windows"',
    ],
)

But this is available only for python_version>=3.4

Also, it looks like https://www.python.org/dev/peps/pep-0508/ doesn't work for me.



Solution 1:[1]

You can use Environment Markers in the specification of your dependencies. See PEP 508.

For detecting if the operating system is Windows or not, you can use os_name being equal to nt.

Based on the syntax in the PEP which shows "name; os_name=='a' or os_name=='b'", your install_requires section can be:

install_requires=[
    "matplotlib",
    "pywin32; os_name=='nt'",
],

There is no need to use extras_require for this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 florisla