【发布时间】:2018-09-12 11:45:02
【问题描述】:
我正在尝试使用subprocess.Popen 调用pyuic5,以便在Windows 上的python 脚本中将qt5 .ui 文件转换为python。
command = "pyuic5 -x " + filein + " -o " + fileout
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=False, cwd=folderPath)
output = process.communicate()
给我以下错误:
Traceback (most recent call last):
File "N:\My Documents\Code\Python Projects\Work projects\PyQtConverter\src\fonctions.py", line 36, in convert_qt_2_py
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=False, cwd=folderPath)
File "C:\Python35\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Python35\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable
问题似乎来自于调用pyuic5(尽管它被认为是 Windows cmd 的有效命令?)。
设置shell=True 解决了这个问题,但我一直在读到这个选项可能存在安全风险,不推荐。我应该以不同的方式做事吗?
【问题讨论】:
-
filein和fileout是什么? -
包含输入和输出文件名的变量。这些文件的路径在 folderPath 变量中
-
它们是绝对路径吗?
-
是的。但正如我上面所说,如果 shell 设置为 true,则该命令有效。同样 subprocess.popen("pyuic5") 返回相同的文件未找到错误,而 subprocess.popen("pyuic5", shell=True) 返回预期的“错误:必须指定一个输入 ui 文件”。我的猜测是 Popen 不知道如何在没有 shell=True 参数的情况下调用 pyuic5 ......这让我有点困惑
-
我认为你应该使用
shell = True,因为pyuic最终是一个.bat,如果你不做验证,你所说的安全风险是真的,例如主要验证是filein 是一个扩展名为 .ui 的 Existing 文件,这样就足够了,但想想看,如果这是一个不好的做法,不要认为 python 会消除选项shell = True。
标签: python-3.x pyqt5 pyuic