我正在尝试用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解决了这个问题,但我一直在读到这个选项可能会带来安全风险,所以不推荐这样做。我应该以不同的方式做事情吗?
发布于 2019-05-12 06:25:19
拿这个吧。在win 10上运行良好;)
用户必须更改gui.ui (variableName: guiNameUi)的名称和outputfile.py (variableName: guiNameUi)的名称
例如: guiNameUi = r'Button.ui‘to guiNameUi = r'yourGuiName.ui’guiNamePy = r'Button.py‘to guiNameUi =r’GuiName.py‘
至少有一个必须像这样更改pyuic5的路径: converter = r'C:\Users\YourPC\Anaconda3\Library\bin‘
转换后,gui的python文件在标准应用程序中打开。
"""
Created on Fri May 10 16:59:26 2019
@author: BlindSide
"""
"""
--version show program's version number and exit
-h, --help show this help message and exit
-p, --preview show a preview of the UI instead of generating code
-o FILE, --output=FILE
write generated code to FILE instead of stdout
-x, --execute generate extra code to test and display the class
-d, --debug show debug output
-i N, --indent=N set indent width to N spaces, tab if N is 0 [default:
4]
-w, --pyqt3-wrapper generate a PyQt v3 style wrapper
Code generation options:
--from-imports generate imports relative to '.'
--resource-suffix=SUFFIX
append SUFFIX to the basename of resource files
[default: _rc]
"""
#Used in converting Script
import os
import subprocess
# python file ConvertUiToPy.py and example.ui lies in same folder
# directory of file
currentWorkingDir = os.getcwd()
#name of GUI.ui
guiNameUi = r'Button.ui'
#name of GUI.py
guiNamePy = r'Button.py'
# concat WorkingDir und GUI
fullpathUi = os.path.join(currentWorkingDir, guiNameUi)
fullpathPy = os.path.join(currentWorkingDir, guiNamePy)
# directory of pyuic5.bat -> eg. Anaconda 3 replace XYZ with userAccount
converter = r'C:\Users\YourPC\Anaconda3\Library\bin'
try:
# change directory from to pyuic5
os.chdir(converter)
print("directory changed... \n...executing conversion")
#execute pyuic5 to convert files
p1 = subprocess.call("pyuic5 -x \"" + fullpathUi + "\" -o \"" + fullpathPy + "\"", shell=True)
#file exists?
if(os.path.isfile(fullpathPy)):
print("...open new gui.py")
#open file in standard app
os.startfile("\"" + fullpathPy + "\"")
else:
#file doesnt exists? throw exception!!!
raise
# handling exeception
except:
print("something went wrong")
# further work in workingDirectory
finally:
print("restore old path")
os.chdir(currentWorkingDir)
print("changed to:", os.getcwd())https://stackoverflow.com/questions/52294361
复制相似问题