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

Github Actions中setup.py操作系统识别异常问题排查与修复

问题分析与修复方案

问题根源

你提供的代码仅导入了distutils.util.get_platform,但未将OPERATING_SYSTEM_NAME赋值为get_platform()的返回结果,导致该变量值不符合预期(比如被错误设置为以win开头的字符串),最终macOS系统错误进入Windows分支,安装了python-magic-bin。

get_platform()的返回格式是明确的:

  • Windows系统返回win32或win-amd64等以win开头的字符串
  • macOS系统返回macosx-xxx格式的字符串
  • Linux系统返回linux-x86_64等格式的字符串

修复方案

你需要先正确获取当前平台信息,再进行分支判断。以下提供两种可靠的实现方式:

方式一:使用distutils.util.get_platform

from distutils.util import get_platform

# 先正确获取平台信息
OPERATING_SYSTEM_NAME = get_platform()

PYTHON_MAGIC_BIN = "python-magic-bin"
PYTHON_MAGIC = "python-magic"
requirements = []

if OPERATING_SYSTEM_NAME.startswith('win'):
    requirements.append(PYTHON_MAGIC_BIN)
else:
    requirements.append(PYTHON_MAGIC)

方式二:使用sys.platform(更简洁)

sys.platform是Python标准库中获取平台信息的常用方式,返回值更直观:

  • Windows返回win32
  • macOS返回darwin
  • Linux返回linux

代码实现:

import sys

PYTHON_MAGIC_BIN = "python-magic-bin"
PYTHON_MAGIC = "python-magic"
requirements = []

if sys.platform.startswith('win'):
    requirements.append(PYTHON_MAGIC_BIN)
else:
    requirements.append(PYTHON_MAGIC)

验证说明

修复后,macOS系统会进入else分支,正确安装python-magic;Windows系统则安装python-magic-bin,完全符合你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 19:10:10