<p>在对<a href="https://doc.qt.io/qtforpython/deployment.html" rel="nofollow noreferrer">build a macOS bundle for a PySide2 application</a>的不同选项进行了大量修改之后,我发现以下步骤基本上是现成的</p>
<p>在macOS Catalina 10.15.7上,使用Python 3.9.1、PySide2 5.15.2和pyinstaller 4.2对使用<code>pyinstaller</code>创建macOS应用程序包的方法进行了测试</p>
<ul>
<li><p>安装pyenv和具有框架支持的最新Python(请参见<a href="https://github.com/pyenv/pyenv" rel="nofollow noreferrer">Pyenv</a>、<a href="https://www.freecodecamp.org/news/manage-multiple-python-versions-and-virtual-environments-venv-pyenv-pyvenv-a29fb00c296f/" rel="nofollow noreferrer">How to manage multiple Python versions and virtual environments</a>了解pyenv的工作原理):</p>
<pre><code>brew install pyenv
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.9.1
</code></pre>
</li>
<li><p>为示例创建文件夹,创建示例pyside应用程序:</p>
<pre><code>mkdir hello && cd hello
nano hello.py
</code></pre>
<p>hello.py:</p>
<pre class="lang-py prettyprint-override"><code>import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("Hello World")
self.button = QPushButton("Click me")
self.button.clicked.connect(self.say_hello)
self.setCentralWidget(self.button)
@Slot()
def say_hello(self, url):
self.button.setText("Hello!")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
</code></pre>
</li>
<li><p>为项目创建venv,安装python软件包:</p>
<pre><code>pyenv local 3.9.1
python3 -m venv venv
source venv/bin/activate
pip install pip pyside2 pyinstaller --upgrade
</code></pre>
</li>
<li><p>尝试使用python解释器运行应用程序:</p>
<pre><code>python3 hello.py
</code></pre>
</li>
<li><p>创建macos应用程序包:</p>
<pre><code>pyinstaller --windowed hello.py
</code></pre>
</li>
<li><p>使用应用程序包运行应用程序:</p>
<pre><code>open dist/hello.app
</code></pre>
</li>
<li><p>检查生成的<code>hello.spec</code>对于构建配置,使用<code>pyinstaller hello.spec</code>重新构建应用程序</p>
</li>
</ul>