解决mac端pycharm执行allure命令报错:returned non-zero exit status 127

1、错误代码:

python 复制代码
    def generate_allure_report(self):
        common = ["allure", "generate", "./reports/allure/temps", "-o", "./reports/allure/report", "--clean"]
        # common = ['allure', '--version']
        try:
            # 使用subprocess.run来执行命令
            result = subprocess.run(
                common,
                shell=True, # mac端使用时需要注释掉,否则报错
                check=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True
            )
            logutil.info("Allure report generated successfully.")
            logutil.info("Output:", result.stdout)

2、代码背景

上述代码是在pytest执行完成后执行allure命令生成测试报告,本身可以通过os.system执行,但是为了符合目前新的规范,个人选择改用了subprocess.run()的方式去写入命令。

本来在window端执行没有任何问题,但是在mac端执行时,则直接报错:

returned non-zero exit status 127

3、报错原因

首先,我在命令行手动执行allure命令是没问题的,所以确定allure安装配置正确。刚开始以为是版本问题,后来切换了其他版本发现也是一样报错,于是百度了一下,发现居然没有人发同样的问题上来,最终,ai了一下,发现了正确原因:

在 macOS 上,subprocess.run 的 shell=True 参数可能导致命令解析问题,尤其是当 allure 命令依赖于环境变量时。

但是原因也不具体,这里解决问题,不探究底层了。

4、解决方式

把shell=True这行注释掉后,就可以正常调用执行了。

所以最终正确代码如下:

python 复制代码
    def generate_allure_report(self):
        common = ["allure", "generate", "./reports/allure/temps", "-o", "./reports/allure/report", "--clean"]
        # common = ['allure', '--version']
        try:
            # 使用subprocess.run来执行命令
            result = subprocess.run(
                common,
                # shell=True, # mac端使用时需要注释掉,否则报错
                check=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True
            )
            logutil.info("Allure report generated successfully.")
            logutil.info("Output:", result.stdout)
相关推荐
love530love2 小时前
用自然语言让 AI Agent 卸载软件 —— 以卸载 Visual Studio 2026 为例
ide·人工智能·windows·agent·visual studio·ai agent·marvis
落寞的电源7 小时前
使用Visual Studio SDK制作GLSL词法着色插件
ide·visual studio
1024小神8 小时前
Mac 副屏快速左 50% 分屏
macos
不瘦80斤不改名10 小时前
HalfCart:基于LBS的1\.5km本地拼单系统全栈实践与踩坑复盘
python·docker·typescript·pycharm
LL34363810 小时前
2026最新5款AI编程工具平替实测|终端与IDE vibe coding迭代优缺点深度对比
ide·ai编程
00后程序员张10 小时前
iOS 打包方式汇总 从 Xcode Archive 到轻量级工具链的 IPA 构建
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
Balabala噗11 小时前
VScode接入MiniMax流程
ide·vscode·编辑器
菜鸟歪歪歪13 小时前
idea中增加api中转站教程(Mac,Window通用)
macos·intellij-idea
小石潭记丶1 天前
本地mac创建秘钥,然后远程登录Linux服务器
linux·服务器·macos
承渊政道1 天前
【Python学习】(了解Python背景知识、搭建 Python 环境以及安装PyCharm)
python·学习·pycharm·搭建python环境