把 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM