[英]Why do I get the error: Cannot run 'uic': “execvp: No such file or directory” for a PySide2 program under macOS Catalina?
I want to execute a program which was written in python using PyQt5 and then migrated to PySide2 under MacOS Catalina.我想执行一个使用 PyQt5 用 python 编写的程序,然后在 MacOS Catalina 下迁移到 PySide2。 With PyQt5 it was executing fine.使用 PyQt5,它运行良好。 I have installed PySide2 like PyQt5:我已经像 PyQt5 一样安装了 PySide2:
python3 -m pip install PyQt5
python3 -m pip install PySide2
macOS was restarted afterwards.之后重新启动了 macOS。
Now, with PySide2 installed, I am getting an error in macOS when I am trying to load an Ui-file.现在,安装 PySide2 后,当我尝试加载 Ui 文件时,在 macOS 中出现错误。
MRE which works under Linux, Windows and macOS Catalina: MRE 适用于 Linux、Windows 和 macOS Catalina:
#!/usr/bin/env python3
# Standard library imports
import sys
# Third party imports
from PySide2.QtCore import (QMetaObject,
Slot)
from PySide2.QtWidgets import (QApplication,
QWidget,
QPushButton,
QHBoxLayout,
QMessageBox)
def show_about():
msg = QMessageBox()
msg.setText("About this app.")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
aboutButton = QPushButton("About")
aboutButton.setObjectName("aboutButton")
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(aboutButton)
self.setLayout(hbox)
QMetaObject.connectSlotsByName(self)
self.setWindowTitle('Buttons')
@Slot()
def on_aboutButton_clicked(self):
show_about()
def main():
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
MRE which fails under macOS Catalina, but works in Linux and Windows: MRE 在 macOS Catalina 下失败,但在 Linux 和 Windows 中有效:
#!/usr/bin/env python3
# Standard library imports
import sys
# Third party imports
from PySide2.QtWidgets import (QApplication,
QMainWindow)
from PySide2.QtUiTools import loadUiType
class MainWindow(QMainWindow, loadUiType("win_main.ui")[0]):
def __init__(self):
super().__init__()
self.setupUi(self)
def main():
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Error:错误:
Python 3.8.5 (/usr/local/bin/python3)
>>> %Run pyside2_win_main.py
Cannot run 'uic': "execvp: No such file or directory" - Exit status QProcess::NormalExit ( 255 )
Check if 'uic' is in PATH
Traceback (most recent call last):
File "/Users/dee/ownCloud3/rma2/pyside2_win_main.py", line 12, in <module>
class MainWindow(QMainWindow, loadUiType("win_main.ui")[0]):
TypeError: 'NoneType' object is not subscriptable
File structure for simplicity of test case:为简化测试用例的文件结构:
/Users/dee/ownCloud3/rma2/pyside2_win_main.py
/Users/dee/ownCloud3/rma2/win_main.ui
PySide2 version 15.5.0, shiboken2==5.15.0. PySide2 版本 15.5.0,shiboken2==5.15.0。
macOS Calatina v10.15.06 macOS Calatina v10.15.06
Python 3.8.5 64bit (downloaded from here , no HomeBrew etc. Python 3.8.5 64bit(从这里下载,没有 HomeBrew 等。
Don't use relative paths as they are susceptible to errors as it will depend on how the script is run, instead build an absolute path:不要使用相对路径,因为它们容易出错,因为这取决于脚本的运行方式,而是构建绝对路径:
import os.path
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
ui_file_path = os.path.join(CURRENT_DIR, "win_main.ui")
Ui_Class, _ = loadUiType(ui_file_path)
class MainWindow(QMainWindow, Ui_Class):
# ...
Also add at the top of your file, since it seems that when python was installed, the option to add the necessary paths to the environment variables was not marked:还要在文件顶部添加,因为似乎在安装python时,未标记将必要路径添加到环境变量的选项:
import PySide2
import os.path
uic_dir = os.path.dirname(PySide2.__file__)
os.environ["PATH"] += os.pathsep + uic_dir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.