是否可以在 setup.py 中表达特定于平台的依赖项,而无需构建特定于平台的 Egg 版本?

发布于 2024-11-16 22:25:44 字数 288 浏览 2 评论 0原文

我们有一个占位符 Egg,它不包含任何代码,其存在只是为了从 PyPi 存储库中提取依赖包列表。

大多数这些依赖包与平台无关,但有些仅在 Win32 平台上使用。

是否可以以某种方式使依赖项具有平台条件,以便我的 install_requires 列表中的给定依赖项仅在 Win32 上安装时才会被下拉?

或者:是否可以指定可选依赖项列表,如果可用,将安装这些依赖项,但如果不可用,也不会导致 easy_install 失败?

We have a placeholder egg that contains no code and only exists for the sake of pulling down a list of dependent packages from our PyPi repository.

Most of these dependent packages are platform-agnostic, however some are only used on Win32 platforms.

Is it possible to somehow make the dependency platform-conditional, so that a given dependency in my install_requires list will only get pulled down when installing on Win32?

Alternatively: Is it possible to specify a list of optional dependencies, that will be installed if available, but will not cause easy_install to fail if they are not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情域 2024-11-23 22:25:44

对于 sdist、egg 和wheel 版本: https:// setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#platform-specific-dependencies

有时,项目可能需要依赖项才能在特定平台上运行。这可能是一个向后移植模块的包,以便它可以在较旧的 python 版本中使用。或者它可能是在特定操作系统上运行所需的包。这将允许项目在多个不同的平台上工作,而无需安装安装项目的平台不需要的依赖项。

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

For sdist, egg and wheel release from : https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#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"'
    ]
)
枯叶蝶 2024-11-23 22:25:44

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_platformsys.platform 拥有更多信息:

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

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'
森罗 2024-11-23 22:25:44

使用 extras_require 分发选项使“win32 支持”成为可选功能:

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

然后在 Windows 上安装时指定 win32 功能:

easy_install mypackage[win32]

这将拉取列出的 pywin32 包作为 mypackage 的“win32”功能的依赖项。

请参阅此处了解更多信息关于可选功能。

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.

半透明的墙 2024-11-23 22:25:44

构建 Egg 时(使用 python setup.py bdist_egg),您可以强制 setuptools/distribute 构建特定于平台的 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
)

然后您可以运行:

# 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

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 run:

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文