将python源文件打包,能在Windows、Linux、
macOS X 等操作系统下直接可运行。
安装: pip install pyinstaller
使用: cmd下的使用:
Microsoft Windows [版本 10.0.18363.1139]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>cd..
C:\Users>cd /d F:\spiders4cxq\taopiaopiao
F:\spiders4cxq\taopiaopiao>pyinstaller -F GUI4Spider.py
对上述的补充说明:
首先切换到目录,cd /d F:\spiders4cxq\taopiaopiao
然后会在该目录F:\spiders4cxq\taopiaopiao下生成EXE文件,
再使用命令pyinstaller -F GUI4Spider.py
文件名可以使用绝对地址或者相对地址,如:
F:\spiders4cxq\taopiaopiao\GUI4Spider.py
或者:taopiaopiao\GUI4Spider.py
下面展示 命令行运行结果:
Microsoft Windows [版本 10.0.18363.1139]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>cd..
C:\Users>cd /d F:\spiders4cxq\taopiaopiao
F:\spiders4cxq\taopiaopiao>pyinstaller -F GUI4Spider.py
76 INFO: PyInstaller: 4.0
77 INFO: Python: 3.7.4
77 INFO: Platform: Windows-10-10.0.18362-SP0
78 INFO: wrote F:\spiders4cxq\taopiaopiao\GUI4Spider.spec
82 INFO: UPX is not available.
85 INFO: Extending PYTHONPATH with paths
['F:\\spiders4cxq\\taopiaopiao', 'F:\\spiders4cxq\\taopiaopiao']
107 INFO: checking Analysis
107 INFO: Building Analysis because Analysis-00.toc is non existent
107 INFO: Initializing module dependency graph...
110 INFO: Caching module graph hooks...
121 INFO: Analyzing base_library.zip ...
3188 INFO: Caching module dependency graph...
3285 INFO: running Analysis Analysis-00.toc
3302 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by d:\python\python37\python.exe
3400 INFO: Analyzing F:\spiders4cxq\taopiaopiao\GUI4Spider.py
3624 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-urllib3.packages.six.moves.py'.
5852 INFO: Processing pre-find module path hook distutils from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
5854 INFO: distutils: retargeting to non-venv dir 'd:\\python\\python37\\lib'
7128 INFO: Processing pre-find module path hook site from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
7129 INFO: site: retargeting to fake-dir 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\fake-modules'
8484 INFO: Processing pre-safe import module hook setuptools.extern.six.moves from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-setuptools.extern.six.moves.py'.
13207 INFO: Processing module hooks...
13207 INFO: Loading module hook 'hook-certifi.py' from 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
13217 INFO: Loading module hook 'hook-pycparser.py' from 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
13218 INFO: Loading module hook 'hook-distutils.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13220 INFO: Loading module hook 'hook-encodings.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13303 INFO: Loading module hook 'hook-lib2to3.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13349 INFO: Loading module hook 'hook-numpy.core.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13461 INFO: Loading module hook 'hook-numpy.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13463 INFO: Loading module hook 'hook-PIL.Image.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13732 INFO: Loading module hook 'hook-PIL.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13734 INFO: Excluding import 'PyQt5'
13737 INFO: Removing import of PyQt5.QtCore from module PIL.ImageQt
13737 INFO: Removing import of PyQt5.QtGui from module PIL.ImageQt
13738 INFO: Import to be excluded not found: 'PyQt4'
13739 INFO: Import to be excluded not found: 'FixTk'
13739 INFO: Excluding import 'tkinter'
13741 INFO: Removing import of tkinter from module PIL.ImageTk
13742 INFO: Import to be excluded not found: 'PySide'
13743 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
13747 INFO: Import to be excluded not found: 'FixTk'
13747 INFO: Excluding import 'tkinter'
13749 INFO: Loading module hook 'hook-pkg_resources.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
15121 INFO: Processing pre-safe import module hook win32com from 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
15371 WARNING: Hidden import "pkg_resources.py2_warn" not found!
15611 INFO: Processing pre-safe import module hook six.moves from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
15897 WARNING: Hidden import "pkg_resources.markers" not found!
15898 INFO: Excluding import '__main__'
15900 INFO: Removing import of __main__ from module pkg_resources
15901 INFO: Loading module hook 'hook-PyQt5.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
16289 WARNING: Hidden import "sip" not found!
16290 INFO: Loading module hook 'hook-PyQt5.QtCore.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
16385 INFO: Loading module hook 'hook-PyQt5.QtGui.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
16601 INFO: Loading module hook 'hook-scipy.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
16607 INFO: Loading module hook 'hook-setuptools.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
17953 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
17955 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
17957 INFO: Loading module hook 'hook-xml.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
17959 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
18128 INFO: checking Tree
18128 INFO: Building Tree because Tree-00.toc is non existent
18130 INFO: Building Tree Tree-00.toc
18164 INFO: checking Tree
18165 INFO: Building Tree because Tree-01.toc is non existent
18166 INFO: Building Tree Tree-01.toc
18173 INFO: Loading module hook 'hook-pythoncom.py' from 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
19341 INFO: Loading module hook 'hook-pywintypes.py' from 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
20571 INFO: Loading module hook 'hook-win32com.py' from 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
21053 INFO: Loading module hook 'hook-packaging.py' from 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
21090 INFO: Looking for ctypes DLLs
21127 INFO: Analyzing run-time hooks ...
21132 INFO: Including run-time hook 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
21136 INFO: Including run-time hook 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
21138 INFO: Including run-time hook 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32comgenpy.py'
21140 INFO: Including run-time hook 'd:\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
21142 INFO: Including run-time hook 'd:\\python\\python37\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\rthooks\\pyi_rth_certifi.py'
21155 INFO: Looking for dynamic libraries
22415 INFO: Looking for eggs
22415 INFO: Using Python library d:\python\python37\python37.dll
22419 INFO: Found binding redirects:
[]
22428 INFO: Warnings written to F:\spiders4cxq\taopiaopiao\build\GUI4Spider\warn-GUI4Spider.txt
22545 INFO: Graph cross-reference written to F:\spiders4cxq\taopiaopiao\build\GUI4Spider\xref-GUI4Spider.html
22585 INFO: checking PYZ
22586 INFO: Building PYZ because PYZ-00.toc is non existent
22586 INFO: Building PYZ (ZlibArchive) F:\spiders4cxq\taopiaopiao\build\GUI4Spider\PYZ-00.pyz
23984 INFO: Building PYZ (ZlibArchive) F:\spiders4cxq\taopiaopiao\build\GUI4Spider\PYZ-00.pyz completed successfully.
24049 INFO: checking PKG
24233 INFO: Building PKG because PKG-00.toc is non existent
24234 INFO: Building PKG (CArchive) PKG-00.pkg
42869 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
42891 INFO: Bootloader d:\python\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
42891 INFO: checking EXE
42892 INFO: Building EXE because EXE-00.toc is non existent
42893 INFO: Building EXE from EXE-00.toc
42896 INFO: Appending archive to EXE F:\spiders4cxq\taopiaopiao\dist\GUI4Spider.exe
42938 INFO: Building EXE from EXE-00.toc completed successfully.
F:\spiders4cxq\taopiaopiao>
补充说明:
命令pyinstaller 执行完毕后,
在当前目录下生成两个文件夹build和dist。
build目录是PyInstaller存储临时文件的目录,可以安全删除。
最终的打包程序在dist内部与源文件同名的目录中,
其他的文件是可执行文件的动态链接库。
可以使用 -F参数对python源文件生成一个独立的可执行文件,
如下:
pyinstaller -F
注意事项:
文件路径中不能出现空格和英文句号(.)
源文件必须是UTF-8编码,暂不支持其他编码类型,
IDLE编写的源文件都保存为UTF-8编码形式,可直接使用
常用参数:
-h, --help 查看帮助
--clean 清理打包过程中的临时文件
-D, --onedir 默认值,生成dist目录
-F,--onefile 在dist文件夹中只生成独立的打包文件
-i <图标文件名.ico> 指定打包程序使用的图标(icon)文件
注意事项:
因为PyInstaller只能分析出需要哪些代码文件。
而你的程序动态打开的资源文件,比如图片、图标等,
它是不会帮你打包的。需要你自己复制粘贴到相应位置,
如此处需要将文件movie.ico复制粘贴到目录dist下。
否则程序运行报错,直接退出。
运行效果展示: