Python PyInstaller安装和使用教程

说明

在创建了独立应用(自包含该应用的依赖包)之后,还可以使用 PyInstaller 将 Python 程序生成可直接运行的程序,这个程序就可以被分发到对应的 Windows 或 Mac OS X 平台上运行。

安装 PyInstaller

pip install pyinstaller

Python PyInstaller安装和使用教程_第1张图片

PyInstaller生成可执行程序

不管这个 Python 应用是单文件的应用,还是多文件的应用,只要在使用 pyinstaller 命令时编译作为程序入口的 Python 程序即可。

增加调用main()函数

def main():
    print('程序开始执行')
    input("请输入")
# 增加调用main()函数
if __name__ == '__main__':
    main()

接下来使用命令行工具进入到此 app 目录下,执行如下命令:

pyinstaller -F app.py

执行上面命令,将看到详细的生成过程。当生成完成后,将会在此 app 目录下看到多了一个 dist 目录,并在该目录下看到有一个 app.exe 文件,这就是使用 PyInstaller 工具生成的 EXE 程序。

PyInstaller 常用选项

Python PyInstaller安装和使用教程_第2张图片

操作结果

PS D:\MyProjectHub> pyinstaller -F app.py
82 INFO: PyInstaller: 3.5
82 INFO: Python: 3.7.5
83 INFO: Platform: Windows-10-10.0.18362-SP0
84 INFO: wrote D:\MyProjectHub\app.spec
86 INFO: UPX is not available.
90 INFO: Extending PYTHONPATH with paths
['D:\\MyProjectHub', 'D:\\MyProjectHub']
90 INFO: checking Analysis
91 INFO: Building Analysis because Analysis-00.toc is non existent
91 INFO: Initializing module dependency graph...
93 INFO: Initializing module graph hooks...
98 INFO: Analyzing base_library.zip ...
4690 INFO: running Analysis Analysis-00.toc
4709 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\applications\python\python.exe
5311 INFO: Caching module hooks...
5316 INFO: Analyzing D:\MyProjectHub\app.py
5319 INFO: Loading module hooks...
5319 INFO: Loading module hook "hook-encodings.py"...
5398 INFO: Loading module hook "hook-pydoc.py"...
5400 INFO: Loading module hook "hook-xml.py"...
5714 INFO: Looking for ctypes DLLs
5715 INFO: Analyzing run-time hooks ...
5719 INFO: Looking for dynamic libraries
5831 INFO: Looking for eggs
5831 INFO: Using Python library d:\applications\python\python37.dll
5831 INFO: Found binding redirects:
[]
5836 INFO: Warnings written to D:\MyProjectHub\build\app\warn-app.txt
5866 INFO: Graph cross-reference written to D:\MyProjectHub\build\app\xref-app.html
5898 INFO: checking PYZ
5898 INFO: Building PYZ because PYZ-00.toc is non existent
5899 INFO: Building PYZ (ZlibArchive) D:\MyProjectHub\build\app\PYZ-00.pyz
6341 INFO: Building PYZ (ZlibArchive) D:\MyProjectHub\build\app\PYZ-00.pyz completed successfully.
6352 INFO: checking PKG
6352 INFO: Building PKG because PKG-00.toc is non existent
6352 INFO: Building PKG (CArchive) PKG-00.pkg
9121 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
9123 INFO: Bootloader d:\applications\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
9123 INFO: checking EXE
9125 INFO: Building EXE because EXE-00.toc is non existent
9125 INFO: Building EXE from EXE-00.toc
9126 INFO: Appending archive to EXE D:\MyProjectHub\dist\app.exe
9189 INFO: Building EXE from EXE-00.toc completed successfully.
PS D:\MyProjectHub>

Python PyInstaller安装和使用教程_第3张图片

Python PyInstaller安装和使用教程_第4张图片
Python PyInstaller安装和使用教程_第5张图片

你可能感兴趣的:(Python)