目录
安装PyInstaller
使用PyInstaller
相信许多人写完Python程序之后想要打包,今天我就教大家使用PyInstaller打包Python程序
安装PyInstaller非常简单执行以下命令即可:
pip install pyinstaller
输出:
Mac@MacBook-Pro ~ % pip install pyinstaller
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyinstaller
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7a/ab/c2b83ebd76b77584c6673e137508529f2027e23578afe74740524f615483/pyinstaller-5.1-py3-none-macosx_10_13_universal2.whl (820 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 820.9/820.9 kB 3.0 MB/s eta 0:00:00
Requirement already satisfied: macholib>=1.8 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pyinstaller) (1.16)
Requirement already satisfied: setuptools in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pyinstaller) (41.2.0)
Requirement already satisfied: altgraph in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pyinstaller) (0.17.2)
Requirement already satisfied: pyinstaller-hooks-contrib>=2021.4 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pyinstaller) (2022.4)
Installing collected packages: pyinstaller
Successfully installed pyinstaller-5.1
Mac@MacBook-Pro ~ %
安装完成之后就可以使用了,PyInstaller打包命令如下:
Pyinstaller -F xxx.py -i xxx.ico
PyInstaller打包常用参数如下:
-w | 不显示运行窗口(cmd窗口) |
-F | 打包为单个exe文件 |
-i | 设置图标 |
打包输出结果:
mac@MacBook-Pro MusicPlayer % pyinstaller -F MusicPlayer.py -w
136 INFO: PyInstaller: 5.1
136 INFO: Python: 3.8.0
148 INFO: Platform: macOS-10.15.7-x86_64-i386-64bit
149 INFO: wrote /Users/Kevin/Desktop/Python/MusicPlayer/MusicPlayer.spec
155 INFO: UPX is not available.
157 INFO: Extending PYTHONPATH with paths
['/Users/Kevin/Desktop/Python/MusicPlayer']
pygame 2.1.2 (SDL 2.0.18, Python 3.8.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
966 INFO: checking Analysis
967 INFO: Building Analysis because Analysis-00.toc is non existent
967 INFO: Initializing module dependency graph...
970 INFO: Caching module graph hooks...
985 INFO: Analyzing base_library.zip ...
6447 INFO: Caching module dependency graph...
6635 INFO: running Analysis Analysis-00.toc
6644 INFO: Analyzing /Users/Kevin/Desktop/Python/MusicPlayer/MusicPlayer.py
7774 INFO: Processing pre-find module path hook distutils from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'.
7775 INFO: distutils: retargeting to non-venv dir '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8'
8794 INFO: Processing pre-safe import module hook setuptools.extern.six.moves from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/pre_safe_import_module/hook-setuptools.extern.six.moves.py'.
9421 INFO: Processing pre-find module path hook site from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/pre_find_module_path/hook-site.py'.
9423 INFO: site: retargeting to fake-dir '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/fake-modules'
12893 INFO: Processing module hooks...
12893 INFO: Loading module hook 'hook-pygame.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pygame/__pyinstaller'...
pygame 2.1.2 (SDL 2.0.18, Python 3.8.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
13053 INFO: Loading module hook 'hook-pkg_resources.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
14163 INFO: Processing pre-safe import module hook win32com from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/pre_safe_import_module/hook-win32com.py'.
14522 WARNING: Hidden import "pkg_resources.py2_warn" not found!
15000 WARNING: Hidden import "pkg_resources.markers" not found!
15003 INFO: Loading module hook 'hook-lib2to3.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
15069 INFO: Loading module hook 'hook-encodings.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
15151 INFO: Loading module hook 'hook-distutils.util.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
15156 INFO: Loading module hook 'hook-setuptools.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16337 INFO: Loading module hook 'hook-platform.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16339 INFO: Loading module hook 'hook-packaging.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16341 INFO: Loading module hook 'hook-pickle.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16346 INFO: Loading module hook 'hook-heapq.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16351 INFO: Loading module hook 'hook-difflib.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16355 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16358 INFO: Import to be excluded not found: 'test'
16358 INFO: Loading module hook 'hook-sysconfig.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16369 INFO: Loading module hook 'hook-xml.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16490 INFO: Loading module hook 'hook-numpy.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16610 INFO: Import to be excluded not found: 'f2py'
16622 INFO: Loading module hook 'hook-distutils.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16624 INFO: Loading module hook 'hook-numpy._pytesttester.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16628 INFO: Loading module hook 'hook-setuptools.msvc.py' from '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks'...
16660 INFO: Looking for ctypes DLLs
16701 INFO: Analyzing run-time hooks ...
16707 INFO: Including run-time hook '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_subprocess.py'
16709 INFO: Including run-time hook '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgres.py'
16714 INFO: Including run-time hook '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py'
16716 INFO: Including run-time hook '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py'
16718 INFO: Including run-time hook '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py'
16735 INFO: Looking for dynamic libraries
pygame 2.1.2 (SDL 2.0.18, Python 3.8.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/building/build_main.py:156: UserWarning: The numpy.array_api submodule is still experimental. See NEP 47.
__import__(package)
18312 INFO: Looking for eggs
18312 INFO: Using Python library /Library/Frameworks/Python.framework/Versions/3.8/Python
18327 INFO: Warnings written to /Users/Kevin/Desktop/Python/MusicPlayer/build/MusicPlayer/warn-MusicPlayer.txt
18391 INFO: Graph cross-reference written to /Users/Kevin/Desktop/Python/MusicPlayer/build/MusicPlayer/xref-MusicPlayer.html
18433 INFO: checking PYZ
18434 INFO: Building PYZ because PYZ-00.toc is non existent
18435 INFO: Building PYZ (ZlibArchive) /Users/Kevin/Desktop/Python/MusicPlayer/build/MusicPlayer/PYZ-00.pyz
19221 INFO: Building PYZ (ZlibArchive) /Users/Kevin/Desktop/Python/MusicPlayer/build/MusicPlayer/PYZ-00.pyz completed successfully.
19236 INFO: EXE target arch: x86_64
19236 INFO: Code signing identity: None
19237 INFO: checking PKG
19237 INFO: Building PKG because PKG-00.toc is non existent
19237 INFO: Building PKG (CArchive) MusicPlayer.pkg
38214 INFO: Building PKG (CArchive) MusicPlayer.pkg completed successfully.
38239 INFO: Bootloader /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PyInstaller/bootloader/Darwin-64bit/runw
38239 INFO: checking EXE
38242 INFO: Building EXE because EXE-00.toc is non existent
38242 INFO: Building EXE from EXE-00.toc
38242 INFO: Copying bootloader EXE to /Users/Kevin/Desktop/Python/MusicPlayer/dist/MusicPlayer
38249 INFO: Converting EXE to target arch (x86_64)
38383 INFO: Removing signature(s) from EXE
38549 INFO: Appending PKG archive to EXE
38672 INFO: Fixing EXE headers for code signing
38703 INFO: Rewriting the executable's macOS SDK version (12.1.0) to match the SDK version of the Python library (10.9.0) in order to avoid inconsistent behavior and potential UI issues in the frozen application.
38709 INFO: Re-signing the EXE
39057 INFO: Building EXE from EXE-00.toc completed successfully.
39063 INFO: checking BUNDLE
39064 INFO: Building BUNDLE because BUNDLE-00.toc is non existent
39064 INFO: Building BUNDLE BUNDLE-00.toc
39114 INFO: Moving BUNDLE data files to Resource directory
39114 INFO: Signing the BUNDLE...
39441 INFO: Building BUNDLE BUNDLE-00.toc completed successfully.
mac@eMacBook-Pro MusicPlayer %
完成后,将在当前目录下的dist文件夹中生成同py文件名的exe文件
在打包过程中产生的其他文件可以删除!