雜亂大全21-打包python(py檔)變成exe檔

  • 前言:本次為pyinstaller的基本練習
tags: 六角學院

主題:雜亂大全21-打包python(py檔)變成exe檔

本篇重點:

  • 練習使用pyinstaller打包python成為exe

安裝模組

1
pip install pyinstaller

提醒

若要能32位元與64位元皆可運行,
就要在Python 32位元的環境下編譯PyInstaller打包exe,
才能在32位元與64位元成功。

參數介紹

-h 來查看參數
-F 打包成一個exe文件
-icon=圖標路徑
-w 使用視窗,無控制台
-c 使用控制台,無視窗
-D 創建一個目錄,包含exe以及文件

提醒

要加在程式最後加上一個input
不然程式會自動結束

1
2
print("=======End=======")
input("press Enter to stop APP...")

範例

  • 將上一篇的opendata打包
  • 直接下指令(CMD)
  • 要先cd到 要打包資料的位置
    1
    pyinstaller -F .\opendata.py

    自動化的步驟:

1.會先建立一個 spec
2.建立build資料夾
3.建立log紀錄檔
4.建立dist資料夾

  • 執行過程
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    (base) C:\Users\liaozz\Desktop>pyinstaller -F .\opendata.py
    94 INFO: PyInstaller: 4.1
    95 INFO: Python: 3.7.1 (conda)
    97 INFO: Platform: Windows-10-10.0.18362-SP0
    99 INFO: wrote C:\Users\liaozz\Desktop\opendata.spec
    103 INFO: UPX is not available.
    113 INFO: Extending PYTHONPATH with paths
    ['C:\\Users\\liaozz\\Desktop', 'C:\\Users\\liaozz\\Desktop']
    171 INFO: checking Analysis
    172 INFO: Building Analysis because Analysis-00.toc is non existent
    173 INFO: Initializing module dependency graph...
    180 INFO: Caching module graph hooks...
    200 INFO: Analyzing base_library.zip ...
    6814 INFO: Caching module dependency graph...
    7026 INFO: running Analysis Analysis-00.toc
    7073 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
    required by c:\users\liaozz\anaconda3\python.exe
    7632 INFO: Analyzing C:\Users\liaozz\Desktop\opendata.py
    9342 INFO: Processing pre-safe import module hook six.moves from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
    12009 INFO: Processing module hooks...
    12010 INFO: Loading module hook 'hook-lxml.etree.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
    12014 INFO: Loading module hook 'hook-difflib.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12016 INFO: Excluding import of doctest from module difflib
    12016 INFO: Loading module hook 'hook-encodings.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12134 INFO: Loading module hook 'hook-heapq.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12137 INFO: Excluding import of doctest from module heapq
    12138 INFO: Loading module hook 'hook-pickle.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12141 INFO: Excluding import of argparse from module pickle
    12141 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12142 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12143 INFO: Loading module hook 'hook-xml.py' from 'c:\\users\\liaozz\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
    12173 INFO: Looking for ctypes DLLs
    12174 INFO: Analyzing run-time hooks ...
    12188 INFO: Looking for dynamic libraries
    12577 INFO: Looking for eggs
    12578 INFO: Using Python library c:\users\liaozz\anaconda3\python37.dll
    12580 INFO: Found binding redirects:
    []
    12587 INFO: Warnings written to C:\Users\liaozz\Desktop\build\opendata\warn-opendata.txt
    12670 INFO: Graph cross-reference written to C:\Users\liaozz\Desktop\build\opendata\xref-opendata.html
    12702 INFO: checking PYZ
    12702 INFO: Building PYZ because PYZ-00.toc is non existent
    12704 INFO: Building PYZ (ZlibArchive) C:\Users\liaozz\Desktop\build\opendata\PYZ-00.pyz
    13843 INFO: Building PYZ (ZlibArchive) C:\Users\liaozz\Desktop\build\opendata\PYZ-00.pyz completed successfully.
    13868 INFO: checking PKG
    13868 INFO: Building PKG because PKG-00.toc is non existent
    13870 INFO: Building PKG (CArchive) PKG-00.pkg
    18294 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
    18299 INFO: Bootloader c:\users\liaozz\anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
    18300 INFO: checking EXE
    18300 INFO: Building EXE because EXE-00.toc is non existent
    18301 INFO: Building EXE from EXE-00.toc
    18302 INFO: Appending archive to EXE C:\Users\liaozz\Desktop\dist\opendata.exe
    18328 INFO: Building EXE from EXE-00.toc completed successfully.

產生檔案

  • 打開dist資料夾就有exe囉!


參考資料

[1]【Python】使用 PyInstaller 將 Python打包成 exe 檔
https://medium.com/pyladies-taiwan/python-%E5%B0%87python%E6%89%93%E5%8C%85%E6%88%90exe%E6%AA%94-32a4bacbe351