網頁

2021/2/23

Python 在Windows使用PyInstaller打包py檔為exe可執行檔 package py script to exe file

在Windows使用PyInstaller工具打包.py檔為.exe可執行檔。

Python .py檔如果要在沒有安裝Python的電腦中執行的話可以打包成可執行檔。例如你用Python寫了一支好用的小工具要分享給你的團隊使用,但其他人的電腦沒有裝Python時,就可以把寫好的python檔包成可執行檔分享出去。

範例環境:

  • Windows 7 64 Bit
  • Python 3.8.7

例如demo.py可輸入並印出文字如下。

demo.py

import sys

while True:
    str = input('輸入文字: ')
    print('輸入的文字:', str)
    exit = input('離開(y/N)')
    if 'Y' == exit.upper():
        sys.exit()
        

打包.py檔可使用PyInstaller這個工具。在命令列執行pip install pyinstaller進行安裝。

C:\>pip install pyinstaller
Collecting pyinstaller
  Downloading pyinstaller-4.2.tar.gz (3.6 MB)
     |████████████████████████████████| 3.6 MB 386 kB/s
...
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.2 pyinstaller-hooks-contrib-2020.11 py
win32-ctypes-0.2.0

PyInstaller安裝好後,把命令列工作目錄移到要打包的檔案路徑,例如demo.py放在D:\test,則把命令列移到D:\test

然後執行pyinstaller --onefile demo.py即可開始打包。
--onefile參數相當於-F,代表打包成一個可執行檔。如果沒加此參數則預設打包成一個資料夾。

D:\test>pyinstaller --onefile demo.py
62 INFO: PyInstaller: 4.2
62 INFO: Python: 3.8.7
63 INFO: Platform: Windows-7-6.1.7601-SP1
68 INFO: wrote D:\test\demo.spec
74 INFO: UPX is not available.
75 INFO: Extending PYTHONPATH with paths
['D:\\test', 'D:\\test']
82 INFO: checking Analysis
82 INFO: Building Analysis because Analysis-00.toc is non existent
82 INFO: Initializing module dependency graph...
88 INFO: Caching module graph hooks...
98 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict.
102 INFO: Analyzing base_library.zip ...
2216 INFO: Processing pre-find module path hook distutils from 'c:\\users\\user\\appdata\\local\\programs\\python\\
python38\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
2217 INFO: distutils: retargeting to non-venv dir 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib
'
4062 INFO: Caching module dependency graph...
4156 INFO: running Analysis Analysis-00.toc
4159 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\user\appdata\local\programs\python\python38\python.exe
4410 INFO: Analyzing D:\test\demo.py
4411 INFO: Processing module hooks...
4411 INFO: Loading module hook 'hook-difflib.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\
\lib\\site-packages\\PyInstaller\\hooks'...
4414 INFO: Excluding import of doctest from module difflib
4415 INFO: Loading module hook 'hook-distutils.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python3
8\\lib\\site-packages\\PyInstaller\\hooks'...
4421 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\py
thon38\\lib\\site-packages\\PyInstaller\\hooks'...
4422 INFO: Excluding import of lib2to3.refactor from module distutils.util
4424 INFO: Loading module hook 'hook-encodings.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python3
8\\lib\\site-packages\\PyInstaller\\hooks'...
4508 INFO: Loading module hook 'hook-heapq.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\\l
ib\\site-packages\\PyInstaller\\hooks'...
4509 INFO: Excluding import of doctest from module heapq
4511 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\
\lib\\site-packages\\PyInstaller\\hooks'...
4562 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\\users\\user\\appdata\\local\\programs\\pyth
on\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
4563 INFO: Excluding import of test from module multiprocessing.util
4563 INFO: Excluding import of test.support from module multiprocessing.util
4566 INFO: Loading module hook 'hook-pickle.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\\
lib\\site-packages\\PyInstaller\\hooks'...
4571 INFO: Excluding import of argparse from module pickle
4572 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python3
8\\lib\\site-packages\\PyInstaller\\hooks'...
4575 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\\users\\user\\appdata\\local\\programs\\py
thon\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
4578 INFO: Loading module hook 'hook-xml.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib
\\site-packages\\PyInstaller\\hooks'...
4615 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\\users\\user\\appdata\\local\\programs\\python\\python38
\\lib\\site-packages\\PyInstaller\\hooks'...
4754 INFO: checking Tree
4755 INFO: Building Tree because Tree-00.toc is non existent
4755 INFO: Building Tree Tree-00.toc
4947 INFO: checking Tree
4947 INFO: Building Tree because Tree-01.toc is non existent
4948 INFO: Building Tree Tree-01.toc
5050 INFO: checking Tree
5050 INFO: Building Tree because Tree-02.toc is non existent
5051 INFO: Building Tree Tree-02.toc
5075 INFO: Looking for ctypes DLLs
5101 INFO: Analyzing run-time hooks ...
5105 INFO: Including run-time hook 'c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages
\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
5112 INFO: Looking for dynamic libraries
5252 INFO: Looking for eggs
5253 INFO: Using Python library c:\users\user\appdata\local\programs\python\python38\python38.dll
5253 INFO: Found binding redirects:
[]
5259 INFO: Warnings written to D:\test\build\demo\warn-demo.txt
5298 INFO: Graph cross-reference written to D:\test\build\demo\xref-demo.html
5307 INFO: checking PYZ
5307 INFO: Building PYZ because PYZ-00.toc is non existent
5308 INFO: Building PYZ (ZlibArchive) D:\test\build\demo\PYZ-00.pyz
5778 INFO: Building PYZ (ZlibArchive) D:\test\build\demo\PYZ-00.pyz completed successfully.
5787 INFO: checking PKG
5789 INFO: Building PKG because PKG-00.toc is non existent
5789 INFO: Building PKG (CArchive) PKG-00.pkg
7630 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
7631 INFO: Bootloader c:\users\user\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\bootloader
\Windows-64bit\run.exe
7632 INFO: checking EXE
7632 INFO: Building EXE because EXE-00.toc is non existent
7635 INFO: Building EXE from EXE-00.toc
7639 INFO: Copying icons from ['c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\Py
Installer\\bootloader\\images\\icon-console.ico']
7642 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
7644 INFO: Writing RT_ICON 1 resource with 3752 bytes
7647 INFO: Writing RT_ICON 2 resource with 2216 bytes
7650 INFO: Writing RT_ICON 3 resource with 1384 bytes
7652 INFO: Writing RT_ICON 4 resource with 37019 bytes
7654 INFO: Writing RT_ICON 5 resource with 9640 bytes
7657 INFO: Writing RT_ICON 6 resource with 4264 bytes
7661 INFO: Writing RT_ICON 7 resource with 1128 bytes
7667 INFO: Updating manifest in D:\test\build\demo\run.exe.525m27bh
7667 INFO: Updating resource type 24 name 1 language 0
7671 INFO: Appending archive to EXE D:\test\dist\demo.exe
7680 INFO: Building EXE from EXE-00.toc completed successfully.

執行完後在同目錄下可看到多了以下檔案及資料夾。

  • __pycache__
  • build
  • dist
  • demo.spec

dist資料夾中的demo.exe即為可執行檔。


或是使用auto-py-to-exe圖形工具來打包,此工具是基於PyInstaller,只是多了圖形介面。

在命令列執行pip install auto-py-to-exe安裝auto-py-to-exe。

C:\>pip install auto-py-to-exe
Collecting auto-py-to-exe
  Downloading auto_py_to_exe-2.8.0-py2.py3-none-any.whl (79 kB)
     |████████████████████████████████| 79 kB 104 kB/s
...
Successfully installed Eel-0.12.4 auto-py-to-exe-2.8.0 bottle-0.12.19 bottle-websocket-0.2.9 cffi-1.14.5 gevent-21.1.2 gevent-websocket-0.10.1 greenlet-1.0.0 pycparser-2.20 whichcraft-0.6.1 zope.event-4.5.0 zope.interface-5.2.0

在命令列輸入auto-py-to-exe開啟視窗介面。

D:\test>auto-py-to-exe

在[Script Location/脚本位置]輸入要打包的python檔位置,選擇[One File/单文件],點選[COVERT .PY TO .EXE/将.PY转换为.EXE]按鈕開始打包,打包完的檔案預設會在執行auto-py-to-exe目錄下的output資料夾。



但有個問題是執行時若是有控制台互動無法在視窗中貼上。



沒有留言:

張貼留言