把 python 文件打包成独立 exe 可执行文件

 

 

 

使用

> pyinstaller -F AATT.py

  

其中,-F 表示打包成单独的 .exe 文件,这时生成的 .exe 文件会比较大,而且运行速度回较慢。仅仅一个 helloworld 程序,生成的文件就 5MB 大。

另外,使用 -i 还可以指定可执行文件的图标;
-w 表示去掉控制台窗口,这在 GUI 界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!

PyInstaller 会对脚本进行解析,并做出如下动作:

1、在脚本目录生成 helloworld.spec 文件;
2、创建一个 build 目录;
3、写入一些日志文件和中间流程文件到 build 目录;
4、创建 dist 目录;
5、生成可执行文件到 dist 目录;

执行流程:

D:\WorkSpace3\EXAMPLE\AATT>pyinstaller -F AATT.py
66 INFO: PyInstaller: 3.4
66 INFO: Python: 3.6.6
69 INFO: Platform: Windows-10-10.0.15063-SP0
75 INFO: wrote D:\WorkSpace3\EXAMPLE\AATT\AATT.spec
76 INFO: UPX is not available.
77 INFO: Extending PYTHONPATH with paths
['D:\\WorkSpace3\\EXAMPLE', 'D:\\WorkSpace3\\EXAMPLE\\AATT']
78 INFO: checking Analysis
139 INFO: Building because D:\WorkSpace3\EXAMPLE\AATT\MatplotlibWidget.py changed
140 INFO: Initializing module dependency graph...
146 INFO: Initializing module graph hooks...
162 INFO: Analyzing base_library.zip ...
7629 INFO: running Analysis Analysis-00.toc
7645 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\program files\python36\python3.exe
8745 INFO: Caching module hooks...
8751 INFO: Analyzing D:\WorkSpace3\EXAMPLE\AATT\AATT.py
8810 INFO: Processing pre-find module path hook   distutils
13023 INFO: Processing pre-find module path hook   site
13040 INFO: site: retargeting to fake-dir 'c:\\program files\\python36\\lib\\site-packages\\PyInstal
ler\\fake-modules'
15677 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
22523 INFO: Processing pre-safe import module hook   six.moves
29522 INFO: Loading module hooks...
29523 INFO: Loading module hook "hook-distutils.py"...
29537 INFO: Loading module hook "hook-encodings.py"...
29786 INFO: Loading module hook "hook-lib2to3.py"...
29797 INFO: Loading module hook "hook-matplotlib.backends.py"...
36605 INFO:   Matplotlib backend "GTK3Agg": ignored
    cairo backend requires that cairocffi or pycairo is installed
37617 INFO:   Matplotlib backend "GTK3Cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
40003 INFO:   Matplotlib backend "MacOSX": ignored
    cannot import name '_macosx'
40713 INFO:   Matplotlib backend "nbAgg": ignored
    No module named 'IPython'
41389 INFO:   Matplotlib backend "Qt4Agg": added
41760 INFO:   Matplotlib backend "Qt4Cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
42306 INFO:   Matplotlib backend "Qt5Agg": added
42702 INFO:   Matplotlib backend "Qt5Cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
43278 INFO:   Matplotlib backend "TkAgg": added
43855 INFO:   Matplotlib backend "TkCairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
44808 INFO:   Matplotlib backend "WebAgg": ignored
    Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "c:\program files\python36\lib\site-packages\matplotlib\backends\backend_webagg.py", line 31,
 in <module>
    import tornado.web
  File "c:\program files\python36\lib\site-packages\tornado\web.py", line 85, in <module>
    from tornado.concurrent import Future, future_set_result_unless_cancelled
  File "c:\program files\python36\lib\site-packages\tornado\concurrent.py", line 43, in <module>
    from concurrent import futures
  File "c:\program files\python36\lib\site-packages\concurrent\futures\__init__.py", line 8, in <mod
ule>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "c:\program files\python36\lib\site-packages\concurrent\futures\_base.py", line 381
    raise exception_type, self._exception, self._traceback
                        ^
SyntaxError: invalid syntax
45306 INFO:   Matplotlib backend "WX": ignored
    No module named 'wx'
45785 INFO:   Matplotlib backend "WXAgg": ignored
    No module named 'wx'
46148 INFO:   Matplotlib backend "WXCairo": ignored
    No module named 'wx'
46560 INFO:   Matplotlib backend "agg": added
46908 INFO:   Matplotlib backend "cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
47469 INFO:   Matplotlib backend "pdf": added
48029 INFO:   Matplotlib backend "pgf": added
48425 INFO:   Matplotlib backend "ps": added
48811 INFO:   Matplotlib backend "svg": added
49281 INFO:   Matplotlib backend "template": added
49994 INFO: Loading module hook "hook-matplotlib.py"...
50413 INFO: Loading module hook "hook-numpy.core.py"...
50506 INFO: Loading module hook "hook-numpy.py"...
50511 INFO: Loading module hook "hook-PIL.Image.py"...
51196 INFO: Loading module hook "hook-PIL.py"...
51202 INFO: Excluding import 'PyQt4'
51204 INFO:   Removing import of PyQt4 from module PIL.ImageQt
51206 INFO: Import to be excluded not found: 'FixTk'
51206 INFO: Excluding import 'tkinter'
51208 INFO:   Removing import of tkinter from module PIL.ImageTk
51209 INFO: Excluding import 'PySide'
51211 INFO:   Removing import of PySide from module PIL.ImageQt
51212 INFO: Excluding import 'PyQt5'
51215 INFO:   Removing import of PyQt5.QtGui from module PIL.ImageQt
51215 INFO:   Removing import of PyQt5.QtCore from module PIL.ImageQt
51216 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
51224 INFO: Excluding import 'tkinter'
51226 INFO: Import to be excluded not found: 'FixTk'
51226 INFO: Loading module hook "hook-pkg_resources.py"...
52083 INFO: Processing pre-safe import module hook   win32com
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
52226 INFO: Processing pre-safe import module hook   win32com
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
52369 INFO: Loading module hook "hook-pydoc.py"...
52374 INFO: Loading module hook "hook-PyQt5.py"...
52552 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
52669 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
52917 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
53373 INFO: Loading module hook "hook-setuptools.py"...
54655 INFO: Loading module hook "hook-sysconfig.py"...
54687 INFO: Loading module hook "hook-xml.dom.domreg.py"...
54697 INFO: Loading module hook "hook-xml.py"...
54706 INFO: Loading module hook "hook-_tkinter.py"...
55076 INFO: checking Tree
55226 INFO: checking Tree
55406 INFO: Looking for ctypes DLLs
55446 INFO: Analyzing run-time hooks ...
55454 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
55476 INFO: Including run-time hook 'pyi_rth__tkinter.py'
55492 INFO: Including run-time hook 'pyi_rth_pkgres.py'
55497 INFO: Including run-time hook 'pyi_rth_qt5.py'
55505 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
55511 INFO: Including run-time hook 'pyi_rth_mpldata.py'
55534 INFO: Looking for dynamic libraries
70686 INFO: Looking for eggs
70686 INFO: Using Python library c:\program files\python36\python36.dll
70686 INFO: Found binding redirects:
[]
70724 INFO: Warnings written to D:\WorkSpace3\EXAMPLE\AATT\build\AATT\warn-AATT.txt
70896 INFO: Graph cross-reference written to D:\WorkSpace3\EXAMPLE\AATT\build\AATT\xref-AATT.html
71040 INFO: checking PYZ
71090 INFO: Building because toc changed
71091 INFO: Building PYZ (ZlibArchive) D:\WorkSpace3\EXAMPLE\AATT\build\AATT\PYZ-00.pyz
73071 INFO: Building PYZ (ZlibArchive) D:\WorkSpace3\EXAMPLE\AATT\build\AATT\PYZ-00.pyz completed su
ccessfully.
73109 INFO: checking PKG
73149 INFO: Building because toc changed
73149 INFO: Building PKG (CArchive) PKG-00.pkg
74290 WARNING: One binary added with two internal names.
74291 WARNING: ('libGLESv2.dll',
 'C:\\program '
 'files\\python36\\lib\\site-packages\\PyQt5\\Qt\\bin\\libGLESv2.dll',
 'BINARY')
74291 WARNING: was placed previously at
74292 WARNING: ('PyQt5\\Qt\\bin\\libGLESv2.dll',
 'C:\\program '
 'files\\python36\\lib\\site-packages\\PyQt5\\Qt\\bin\\libGLESv2.dll',
 'BINARY')
108230 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
108293 INFO: Bootloader c:\program files\python36\lib\site-packages\PyInstaller\bootloader\Windows-6
4bit\run.exe
108293 INFO: checking EXE
108327 INFO: Rebuilding EXE-00.toc because AATT.exe missing
108328 INFO: Building EXE from EXE-00.toc
108329 INFO: Appending archive to EXE D:\WorkSpace3\EXAMPLE\AATT\dist\AATT.exe
110075 INFO: Building EXE from EXE-00.toc completed successfully.

 

注意事项

1、直接运行最终的 .exe 程序,可能会出现一闪而过的情况,这种情况下要么是程序运行结束(比如直接打印的 helloWorld),要么程序出现错误退出了。

这种情况下,建议在命令行 cmd 下运行 .exe 文件,这时就会有文本输出到窗口;

2、-i 是改变图标的,但是我发现是有些 bug 的,客官请看:

图标变化

放大过程中,图标才变成了我们设置的图标。

3、写代码的时候应当有个良好的习惯,用什么函数导什么函数,不要上来 import 整个库,最后你会发现你一个 100KB 的代码打包出来有 500MB;

4、当你的代码需要调用一些图片和资源文件的,这是不会自动导入的,需要你自己手动复制进去才行。不然 exe 文件运行时命令窗口会报错找不到这个文件。

导入方法:

假设程序中需要引入一个 test.txt 文件,首先我们运行:

pyi-makespec -F helloworld.py

此时会生成一个 .spec 文件,这个文件会告诉 pyinstaller 如何处理你的脚本,pyinstaller 创建一个 exe 的文件就是依靠它里面的内容进行执行的。

正常情况下你不需要去修改这个 spec 文件,除非你需要打包一个 dll 或者 so 文件或者其他数据文件。

那么我们就需要修改这个 spec 文件:

a = Analysis(['helloworld.py'],
             pathex=['/home/test'],
             binaries=[],
             datas=[],  ### <------- 改

修改为:

a = Analysis(['helloworld.py'],
             pathex=['/home/test'],
             binaries=[],
             datas=[('test.txt','.')],  ## <---- 修改此处添加外部文件

然后在生成 exe 文件:

pyinstaller helloworld.spec

然后生成的文件就可以正常引入外部文件了。

 

 

转自:https://www.cnblogs.com/hoxis/p/9699151.html

posted @ 2018-09-26 17:25  iSZ  阅读(5081)  评论(0编辑  收藏  举报