解决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)
相关推荐
goodmao7 小时前
【macOS】【mouse】设置鼠标大小
macos·计算机外设·鼠标·mouse
Magnum Lehar8 小时前
macos信息采集器appledataharvester-3
macos·网络安全·系统安全
Serendipity_Carl9 小时前
1637加盟网数据实战(数分可视化)
爬虫·python·pycharm·数据可视化·数据清洗
川川菜鸟10 小时前
Claude Code 安装与配置完整指南(Mac)
macos
WarmSword10 小时前
mac上用cursor/vscode调试root权限进程
c++·ide·vscode·macos·mac
Magnum Lehar11 小时前
macos的信息采集器appledataHarvester-2
macos·网络安全·系统安全
HAPPY酷12 小时前
构建即自由:一份为创造者设计的 Windows C++ 自动化构建指南
开发语言·c++·ide·windows·python·策略模式·visual studio
echo-niuben12 小时前
macOS 端已如何正常安装并配置XcodeBuildMCP ?
macos
刘某某.13 小时前
Mac上缺失宋体字体,只有宋体-简
macos
henysugar14 小时前
Android studio编译aidl若干问题记录
android·ide·android studio·aidl