📜  导出烧瓶应用程序 - Python (1)

📅  最后修改于: 2023-12-03 14:53:42.064000             🧑  作者: Mango

导出烧瓶应用程序 - Python

在Python中创建GUI应用程序时,PyQt是一个非常流行的工具。它是一个面向对象的框架,基于Qt库,可以帮助开发者构建漂亮的用户界面,而且可以在不同操作系统上使用。在本文中,我们将介绍如何导出烧瓶应用程序。

安装PyInstaller

PyInstaller是一个Python库,它用于将Python应用程序转换为可执行文件。我们可以使用pip安装PyInstaller,命令如下:

pip install pyinstaller

安装完成后,我们已经具备将Python程序打包的基本条件。

打包你的Python程序

要实际打包你的Python程序,我们需要完成以下步骤:

  1. 编写Python应用程序,并确保它能够正常运行。为了演示便利,让我们从一个简单的烧瓶应用程序开始。下面是代码:
import sys
from PyQt5.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
label = QLabel("Hello, Flaskr!")
label.show()
sys.exit(app.exec_())
  1. 将Python脚本保存为示例.py。

  2. 打开命令行,并转到示例.py所在的目录。

  3. 运行以下命令:

pyinstaller --onefile --windowed --icon=icon.ico example.py

其中,

  • --onefile:表示将所有代码和依赖库打包到一个单独的可执行文件中。

  • --windowed:表示在不启动控制台窗口的情况下启动应用程序。

  • --icon:表示为程序添加图标。在上面的代码中,我为应用程序添加了名为icon.ico的图标。

当程序构建时,你将看到以下输出:

99 INFO: PyInstaller: 3.6
99 INFO: Python: 3.7.6
100 INFO: Platform: Windows-10-10.0.18362-SP0
102 INFO: wrote C:\Users\user\example.spec
105 INFO: UPX is not available.
111 INFO: Extending PYTHONPATH with paths
['C:\\Users\\user', 'C:\\Users\\user']
111 INFO: checking Analysis
111 INFO: Building Analysis because Analysis-02.toc is non existent
111 INFO: Initializing module dependency graph...
112 INFO: Caching module graph hooks...
118 INFO: Analyzing base_library.zip ...
4480 INFO: Processing pre-find module path hook   distutils
4482 INFO: distutils: retargeting to non-venv dir 'c:\\python37\\lib'
8265 INFO: Caching module dependency graph...
8369 INFO: running Analysis Analysis-02.toc
8374 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python37\python.exe
8507 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8545 WARNING: lib not found: api-ms-win-core-datetime-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8561 WARNING: lib not found: api-ms-win-core-string-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8578 WARNING: lib not found: api-ms-win-core-file-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8595 WARNING: lib not found: api-ms-win-core-localization-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8613 WARNING: lib not found: api-ms-win-core-synch-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8630 WARNING: lib not found: api-ms-win-core-timezone-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8647 WARNING: lib not found: api-ms-win-core-processenvironment-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8664 WARNING: lib not found: api-ms-win-core-file-l2-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8678 WARNING: lib not found: api-ms-win-core-heap-l1-1-0.dll dependency of c:\python37\bin\Qt5Core.dll
8697 WARNING: lib not found: api-ms-win-core-file-l2-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8710 WARNING: lib not found: api-ms-win-core-heap-l1-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8732 WARNING: lib not found: api-ms-win-core-string-l2-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8746 WARNING: lib not found: api-ms-win-core-synch-l1-2-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8765 WARNING: lib not found: api-ms-win-core-localization-l1-2-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8787 WARNING: lib not found: api-ms-win-core-registry-l1-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8804 WARNING: lib not found: api-ms-win-core-errorhandling-l1-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8820 WARNING: lib not found: api-ms-win-core-libraryloader-l1-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8843 WARNING: lib not found: api-ms-win-core-profile-l1-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8860 WARNING: lib not found: ext-ms-win-gdi-dc-0001.dll dependency of c:\python37\bin\Qt5Gui.dll
8875 WARNING: lib not found: ext-ms-win-gdi-dc-0001.dll dependency of c:\python37\bin\Qt5Core.dll
8893 WARNING: lib not found: api-ms-win-core-processthreads-l1-1-1.dll dependency of c:\python37\bin\Qt5Gui.dll
8908 WARNING: lib not found: api-ms-win-core-handle-l1-1-0.dll dependency of c:\python37\bin\Qt5Gui.dll
8945 INFO: Analyzing C:\Users\user\AppData\Roaming\Python\Python37\site-packages\PyQt5\Qt\bin\QtWidgets.pyd
49528 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
54883 INFO: Processing pre-safe import module hook   six.moves
55092 INFO: Processing pre-safe import module hook   win32timezone
55492 INFO: Processing pre-safe import module hook   win32ctypes
59224 INFO: Processing module hooks...
59228 INFO: Loading module hook "hook-_tkinter.py"...
59374 INFO: Loading module hook "hook-distutils.py"...
59378 INFO: Loading module hook "hook-encodings.py"...
59608 INFO: Loading module hook "hook-importlib_metadata.py"...
59613 INFO: Loading module hook "hook-numpy.core.py"...
59970 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
60014 INFO: Loading module hook "hook-numpy.py"...
60024 INFO: Loading module hook "hook-PIL.Image.py"...
60031 INFO: Loading module hook "hook-PIL.py"...
60033 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
60035 INFO: Loading module hook "hook-pkg_resources.py"...
60151 INFO: Processing module hooks...
60151 INFO: Loading module hook "hook-PyQt5.py"...
60228 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
60233 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
60233 INFO: Loading module hook "hook-PyQt5.QtMultimedia.py"...
60238 INFO: Loading module hook "hook-PyQt5.QtMultimediaWidgets.py"...
60243 INFO: Loading module hook "hook-PyQt5.QtSvg.py"...
60247 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
60250 INFO: Loading module hook "hook-PySide2.py"...
60260 INFO: Loading module hook "hook-scipy.py"...
60275 INFO: Loading module hook "hook-SQLAlchemy.py"...
60278 INFO: Loading module hook "hook-sysconfig.py"...
60279 INFO: Loading module hook "hook-xml.etree.ElementTree.py"...
60280 INFO: Loading module hook "hook-_tkinter.py"...
60284 INFO: checking Tree
60285 INFO: Building Tree because Tree-02.toc is non existent
60286 INFO: Building Tree Tree-02.toc
60479 INFO: checking Tree
60479 INFO: Building Tree because Tree-03.toc is non existent
60479 INFO: Building Tree Tree-03.toc
61168 INFO: Loading module hook "hook-graphviz.py"...
61197 INFO: Loading module hook "hook-lxml.etree.py"...
61200 INFO: Loading module hook "hook-PIL.ImageTk.py"...
61203 INFO: Loading module hook "hook-PyQt5.QtPrintSupport.py"...
61240 INFO: Loading module hook "hook-zmq.py"...
61268 INFO: Loading module hook "hook-nbconvert.py"...
61301 INFO: Loading module hook "hook-nbformat.py"...
61332 INFO: Loading module hook "hook-notebook.py"...
62274 INFO: Looking for ctypes DLLs
62504 INFO: Analyzing run-time hooks ...
62528 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
62544 INFO: Including run-time hook 'pyi_rth_pkgres.py'
62554 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
62571 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
62593 INFO: Including run-time hook 'pyi_rth_nltk.py'
62614 INFO: Including run-time hook 'pyi_rth_jinja2.py'
62645 INFO: Including run-time hook 'pyi_rth_inspect.py'
62649 INFO: Including run-time hook 'pyi_rth_traitlets.py'
62665 INFO: Including run-time hook 'pyi_rth_mpldata.py'
62689 INFO: Including run-time hook 'pyi_rth_gensim.py'
62713 INFO: Including run-time hook 'pyi_rth_pandas.py'
62739 INFO: Including run-time hooks from C:\Python37\lib\site-packages\PyInstaller\hooks\rthooks
62740 INFO: Import to be excluded not found: 'bleach'
62740 INFO: Import to be excluded not found: 'decorator'
62740 INFO: Import to be excluded not found: 'html.entities'
62740 INFO: Loading module hook "hook-pytz.py"...
62787 INFO: Loading module hook "hook-sphinx.py"...
62788 INFO: Loading module hook "hook-sphinx.util.py"...
62789 INFO: Loading module hook "hook-zmq.utils.py"...
62791 INFO: Loading module hook "hook-setuptools.py"...
63254 INFO: Loading module hook "hook-PyQt5.QtWebEngineWidgets.py"...
63259 INFO: Loading module hook "hook-lxml.etree.py"...
63261 INFO: Loading module hook "hook-numpy.random.py"...
63630 INFO: Generating spec file for c:\users\user\example.py
63670 INFO: Generating executable for c:\users\user\example.py
63752 INFO: Skipping checks
63758 INFO: Building because toc changed
63758 INFO: Building EXE from out00-EXE.toc
63784 INFO: Appending archive to EXE C:\Users\user\dist\example.exe

在完成过程后,你会在dist目录中看到一个名为example.exe的文件。这就是已经打包好的可执行文件了。

总结

在本文中,我们介绍了如何使用PyInstaller将Python应用程序导出为独立的可执行文件。如果你已经掌握了基本的PyQt编程技术,那么使用PyInstaller工具打包应用程序是非常简单的。希望这篇文章对你有所帮助!