You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在setup.py中添加平台特定包数据,构建对应平台的wheel安装包?

解决方案

你可以通过动态判断构建平台+按需复制对应文件+指定wheel为平台专属的方式实现需求,具体操作如下:

步骤1:编写setup.py逻辑

核心逻辑是在构建阶段识别当前环境的平台信息,将对应目录下的绑定文件和二进制文件纳入打包范围,再配置参数生成平台专属wheel。以下是两种实现方案:

方案1:简单版(适合本地构建)

直接将对应平台文件复制到源码目录后打包:

import os
import shutil
import platform
from setuptools import setup, find_packages

# 1. 识别当前构建平台,匹配你data目录的命名规则
system = platform.system().lower()
arch = platform.machine().lower()

system_map = {
    "darwin": "macos",
    "linux": "linux",
    "windows": "windows"
}
arch_map = {
    "x86_64": "x64",
    "amd64": "x64",
    "arm64": "arm64",
    "aarch64": "arm64"
}

platform_dir = f"{system_map[system]}-{arch_map[arch]}"
platform_data_path = os.path.join("data", platform_dir)
target_package_path = os.path.join("src", "package_name")

# 2. 复制对应平台的文件到源码包目录
if not os.path.exists(platform_data_path):
    raise RuntimeError(f"未找到当前平台 {platform_dir} 对应的资源文件")
for filename in os.listdir(platform_data_path):
    src_file = os.path.join(platform_data_path, filename)
    dst_file = os.path.join(target_package_path, filename)
    shutil.copy2(src_file, dst_file)

# 3. 配置打包参数
setup(
    name="package_name",
    version="0.1.0",
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    # 配置包含二进制文件和绑定文件
    package_data={
        "package_name": ["binary.*", "_bindings.py"]
    },
    # 指定生成平台专属wheel,禁用通用wheel
    options={
        "bdist_wheel": {
            "universal": False
        }
    },
    # 其他配置:作者、描述、依赖等按需补充
)

方案2:优化版(不污染源码目录,适合CI构建)

重写构建命令,在构建阶段动态将文件复制到构建临时目录,不修改本地源码:

import os
import shutil
import platform
from setuptools import setup, find_packages
from setuptools.command.bdist_wheel import bdist_wheel as _bdist_wheel

# 平台识别逻辑和上面一致
system = platform.system().lower()
arch = platform.machine().lower()
system_map = {"darwin": "macos", "linux": "linux", "windows": "windows"}
arch_map = {"x86_64": "x64", "amd64": "x64", "arm64": "arm64", "aarch64": "arm64"}
platform_dir = f"{system_map[system]}-{arch_map[arch]}"
platform_data_path = os.path.join("data", platform_dir)

if not os.path.exists(platform_data_path):
    raise RuntimeError(f"未找到当前平台 {platform_dir} 对应的资源文件")

class CustomBdistWheel(_bdist_wheel):
    def run(self):
        # 先执行默认构建逻辑
        super().run()
        # 找到构建后的临时包目录
        build_lib = self.build_lib
        target_path = os.path.join(build_lib, "package_name")
        # 复制对应平台文件到构建目录
        for filename in os.listdir(platform_data_path):
            src_file = os.path.join(platform_data_path, filename)
            dst_file = os.path.join(target_path, filename)
            shutil.copy2(src_file, dst_file)

setup(
    name="package_name",
    version="0.1.0",
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    options={
        "bdist_wheel": {
            "universal": False
        }
    },
    # 绑定自定义构建命令
    cmdclass={
        "bdist_wheel": CustomBdistWheel
    }
)

步骤2:执行构建

在对应平台的环境中执行以下命令即可生成对应平台的专属wheel包:

python setup.py bdist_wheel

生成的wheel包会保存在dist目录下,文件名会自带平台标识,用户安装时pip会自动匹配与本地系统一致的包。

验证方法

构建完成后可以直接解压wheel包查看内部结构,确认仅包含当前平台的_bindings.py和对应后缀的二进制文件,和预期的目录结构一致。

内容的提问来源于stack exchange,提问作者mara004

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 02:06:04