PyInstaller打包后subprocess.run无法执行,报错3221226505求助
PyInstaller打包后subprocess无法执行MayaBatch的问题
代码内容
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton from PyQt5.QtCore import pyqtSlot import subprocess def create_and_write_txt(*args): cmd = '"C:/Program Files/Autodesk/Maya2022/bin/mayabatch.exe" -file "E:/Maya_Project/scenes/input/null_scene.mb" -script "E:/Maya_Project/scenes/output/LuanchMelScript.mel" "-noAutoloadPlugins"' subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) class App(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt5 Button Example") self.resize(300, 200) self.button = QPushButton("Execute", self) self.button.clicked.connect(self.on_click) @pyqtSlot() def on_click(self): create_and_write_txt() app = QApplication([]) ex = App() ex.show() app.exec_()
问题描述
上述代码在PyCharm中运行正常,但通过PyInstaller打包为可执行文件后,subprocess.run无法执行。
使用的打包命令:
pyinstaller -D E:\Python3.10_Project\Batch_Tool.py -i E:\Python3.10_Project\image\Batch_Tool.ico
运行生成的Batch_Tool.exe时出现错误:返回非零退出状态3221226505

解决建议
- 取消
shell=True,改用参数列表传递命令:shell=True会依赖系统shell环境,打包后易出现路径或环境变量问题,改用列表形式传递参数更稳定:def create_and_write_txt(*args): cmd = [ "C:/Program Files/Autodesk/Maya2022/bin/mayabatch.exe", "-file", "E:/Maya_Project/scenes/input/null_scene.mb", "-script", "E:/Maya_Project/scenes/output/LuanchMelScript.mel", "-noAutoloadPlugins" ] subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) - 检查权限:尝试以管理员身份运行
Batch_Tool.exe,避免程序因权限不足无法访问Maya安装目录或项目文件。 - 捕获错误输出排查:修改代码打印
stderr内容,打包后运行查看具体错误信息,精准定位问题:result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, text=True) print("标准输出:", result.stdout) print("错误输出:", result.stderr) - 强制指定shell路径(若必须用
shell=True):在subprocess.run中添加executable参数指定系统cmd路径,比如:subprocess.run(cmd, shell=True, executable="C:/Windows/System32/cmd.exe", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
内容的提问来源于stack exchange,提问作者xh c
相关产品推荐
相关产品推荐

