Is it possible to express a platform-specific dependency in setup.py without building platform-specific versions of my egg?

最后都变了- 提交于 2019-11-28 09:45:32

For sdist, egg and wheel release from : http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies

Sometimes a project might require a dependency to run on a specific platform. This could to a package that back ports a module so that it can be used in older python versions. Or it could be a package that is required to run on a specific operating system. This will allow a project to work on multiple different platforms without installing dependencies that are not required for a platform that is installing the project.

setup(
    name="Project",
    ...
    install_requires=[
        'enum34 ; python_version<"3.4"',
        'pywin32 >= 1.0 ; platform_system=="Windows"'
    ]
)
codeape

In setup.py:

from setuptools import setup
import sys

setup(
    name="...",
    install_requires=["This", "That"] + (
        ["WinOnly", "AnotherWinOnly"] if sys.platform.startswith("win") else []
        )
)

distutils.util.get_platform has more information than sys.platform if you need it:

>>> sys.platform
'linux2'
>>> distutils.util.get_platform()
'linux-i686'

Use the extras_require distribution option to make 'win32 support' an optional feature:

setup(
  ...
  extras_require={
    'win32': 'pywin32'
  },
  ...
)

Then specify the win32 feature when installing on Windows:

easy_install mypackage[win32]

This will pull down the pywin32 package, which is listed as a dependency for the 'win32' feature of mypackage.

See here for more information about optional features.

When the egg is built (using python setup.py bdist_egg), you can force setuptools/distribute to build a platform-specific egg.

from setuptools import setup
import os

# Monkey-patch Distribution so it always claims to be platform-specific.
from distutils.core import Distribution
Distribution.has_ext_modules = lambda *args, **kwargs: True

requirements = ['generic-foo', 'generic-bar']

if os.getenv('WINDOWS_BUILD'):
    requirements.extend(['a-windows-only-requirement'])

setup(
    name="...",
    install_requires=requirements
)

You can then simply do:

# Force a windows build
$ WINDOWS_BUILD=y python setup.py bdist_egg -p win32
# Do a linux build -- you may not need to specify -p if you're happy
# with your current linux architecture.
$ python setup.py bdist_egg -p linux-i686
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!