解决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)
相关推荐
pe7er3 天前
macOS 应用无法打开(权限问题)解决方案
macos·mac
QQ_19632884755 天前
Python-flask框架西山区家政服务评价系统网站设计与开发-Pycharm django
python·pycharm·flask
Diligently_5 天前
idea 中vm option 配置
java·ide·intellij-idea
我命由我123455 天前
在 Android Studio 中,新建 AIDL 文件按钮是灰色
android·ide·android studio·安卓·android jetpack·android-studio·android runtime
AC赳赳老秦5 天前
云原生AI故障排查新趋势:利用DeepSeek实现高效定位部署报错与性能瓶颈
ide·人工智能·python·云原生·prometheus·ai-native·deepseek
被制作时长两年半的个人练习生5 天前
claude code for vscode 配置 qwen3.5
ide·vscode·claude code·qwen3.5
圣心5 天前
Visual Studio Code 中的 AI 智能操作
ide·人工智能·vscode
harmful_sheep5 天前
mac生效的终端查看
macos
吹牛不交税5 天前
关于vscode左侧资源管理器目录层级疑似异常的问题
ide·vscode·编辑器
xixi09245 天前
selenium IDE安装使用教程
ide·selenium·测试工具