Why do I get the error: Cannot run 'uic': "execvp: No such file or directory" for a PySide2 program under macOS Catalina?

Why do I get the error: Cannot run 'uic': "execvp: No such file or directory" for a PySide2 program under macOS Catalina?

我想执行一个使用 PyQt5 在 python 中编写的程序,然后在 MacOS Catalina 下迁移到 PySide2。 使用 PyQt5,它执行得很好。 我已经像 PyQt5 一样安装了 PySide2:

python3 -m pip install PyQt5
python3 -m pip install PySide2

之后重新启动了 macOS。

现在,安装 PySide2 后,我在 macOS 中尝试加载 Ui 文件时出现错误。

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 在 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()

错误:

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

简化测试用例的文件结构:

/Users/dee/ownCloud3/rma2/pyside2_win_main.py
/Users/dee/ownCloud3/rma2/win_main.ui

PySide2版本15.5.0,shiboken2==5.15.0.

macOS Calatina v10.15.06

Python 3.8.5 64 位(从 here 下载,没有 HomeBrew 等

不要使用相对路径,因为它们容易出错,因为这将取决于脚本的方式 运行,而是构建绝对路径:

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):
    # ...

同样在你的文件顶部添加,因为安装python时似乎没有标记将必要路径添加到环境变量的选项:

import PySide2 
import os.path  

uic_dir = os.path.dirname(PySide2.__file__) 
os.environ["PATH"] += os.pathsep + uic_dir