Setuptools platform specific dependencies

会有一股神秘感。 提交于 2019-12-03 07:37:39

setup.py is simply a python script. You can create dynamic dependencies in that script:

import platform

setup_requires = ['foo', 'bar']

if platform.system() == 'Windows':
    setup_requires.append('pyreadline')

setup(
    # ...
    setup_requires=setup_requires,
)
Remco Haszing

While the answer given by Martijn Pieters was totally valid at the time, Python packaging has changed a lot since then.

The preferred format to distribute packages is using wheels*. Using wheels it is not possible to run Python code during installation.

Wheel use metadata version two as specified in PEP 0427. Environment markers can be used to specify platform specific dependencies.

Setuptools allows to specify these environment markers as extras_require keys. The following example script depends on pyreadline for Windows systems and on pyxdg for Linux distributions.

#!/usr/bin/env python
from setuptools import setup

setup(
    name='spam',
    version='0.0.1',
    extras_require={
        ':sys_platform == "win32"': [
            'pyreadline'
        ],
        ':"linux" in sys_platform': [
            'pyxdg'
        ]
    })

*Also release an sdist, so platforms which can't use wheel can still install your package.

Other answers are valid and probably more convenient if supporting old setuptools versions is required, but there have been some advancements:

Recent versions of setuptools accept PEP 508 style dependency specification:

setup(
    # ...
    install_requires=[
        'pyreadline; platform_system == "Windows"',
    ],
)

Choose the right parameter:

  • install_requires: what other distributions are needed for the current distribution to work correctly
  • extras_require: a dictionary mapping the names of optional features to a list of their requirements
  • setup_requires: other distributions that need to be present for the setup script to run correctly Note: projects listed in setup_requires will NOT be automatically installed. They are simply downloaded to the ./.eggs directory if they’re not locally available already.

There is also an alternative way for supplying these parameters through setup.cfg file. See the documentation for more info.

PEP 518 introduces a new and more capable way of specifying setup_requires in pyproject.toml file:

[build-system]
# Minimum requirements for the build system to execute.
requires = ['setuptools>"38.3.0"', 'wheel']  # PEP 508 specifications.

The feature was implemented in pip 10.0.0b1. Using it one will be able to automatically install and update build system requirements.

from setuptools import setup


setup(
    install_requires=(['pymsgbox', 'PyTweening>=1.0.1', 'Pillow', 'pyscreeze']
                    + ["python3-xlib; sys_platform == linux"]
                    + ["python-xlib; sys_platform == linux2"]
                    + ["pyobjc-core; sys_platform == darwin"]
                    + ["pyobjc; sys_platform == darwing"]
                    ),
)

This will install speicific versions of libraries depending on whether its linux2 (for a linux system using python2), linux (for a linux system using python3), darwin (for a MacOS system)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!