程式打包終於成功了-Python程式打包失敗的經歷

墮落小生發表於2017-05-27

今天瀏覽以前在社群發表的文章,看到這個失敗經歷,滿滿的血淚啊,好在終於找到了方法,可以把辛苦編寫的程式打包成exe,分享給小夥伴,在這裡把方法記錄下來.

這個方法我只試過 python + pyqt 編寫的程式, 打包工具使用的是 pyinstaller . 以前一直打包失敗的原因,有一點是一直想把程式打包成單檔案的 exe ,結果一直沒有成功,所以,不要打包成單檔案.

打包方法:

1. 使用 pyinstaller 打包程式後,進入你安裝pyqt的路徑,找到 ./plugins/platforms 目錄,這個目錄下包含所需的qwindows.dll檔案,複製platforms資料夾,貼上到打包後的資料夾中的qt4_plugins目錄下  

2. 把設計好的UI檔案複製到打包後的資料夾中,我沒有把ui檔案轉換成python,而是直接用的。

3. 雙擊exe檔案。程式應該可以正常執行了。  

最近學習Python程式設計,寫了一個小程式,想把程式打包成exe可執行檔案,經過各種折騰之後,讓我一度以為我是不是應該放棄打包這個事情,專心學習程式設計。

系統環境
OS 名稱: Microsoft Windows XP Professional
OS 版本: 5.1.2600 Service Pack 3 Build 2600
OS 製造商: Microsoft Corporation
Python version: 3.4.2
PyQt4 version:PyQt GPL v4.11.4 for Python v3.4 (x32)

程式原始碼:https://git.oschina.net/duoluoxiaosheng/zhwnl_weather

打包工具1: cx_Freezer:

打包結果:
enter image description here
不知道跟這些提示有沒有關係:
Missing modules:
? _dummy_threading imported from dummy_threading
? _scproxy imported from urllib.request
? ce imported from os
? doctest imported from heapq
? grp imported from shutil, tarfile
? org.python.core imported from copy
? os.path imported from os, py_compile, shutil
? posix imported from os
? pwd imported from getpass, posixpath, shutil, tarfile
? subprocess imported from os
? termios imported from getpass
This is not necessarily a problem - the modules may not be needed on this platform.

setup.py 程式碼:

import sys  
from cx_Freeze import setup, Executable  
base = None  
if sys.platform == "win32":  
    base = "Win32GUI"  
setup(
    name = "simple_PyQt4",  
    version = "0.1",  
    description = "Sample cx_Freeze PyQt4 script",  
    executables = [Executable("weather_zhwnl.py", base = base)]
)  

打包工具2:py2exe

enter image description here

下面是打包過程:

running py2exe   

    1 missing Modules  
    ------------------  
? readline                            imported from cmd, code, pdb  
Building 'dist\weather_zhwnl.exe'.  
Copy DLL C:\Python34\lib\site-packages\PyQt4\Qt5PrintSupport.dll to dist  
Copy DLL C:\Python34\lib\site-packages\PyQt4\Qt5Gui.dll to dist  
Copy DLL C:\Python34\lib\site-packages\PyQt4\Qt5Widgets.dll to dist  
Copy DLL C:\Python34\lib\site-packages\PyQt4\Qt5Core.dll to dist  
Copy DLL C:\Python34\lib\site-packages\PyQt4\icudt53.dll to dist  
Copy DLL C:\Python34\lib\site-packages\PyQt4\icuin53.dll to dist  
Copy DLL C:\Python34\lib\site-packages\PyQt4\icuuc53.dll to dist  

setup.py 程式碼:

from distutils.core import setup  
import py2exe  
import sys  
#this allows to run it with a simple double click.  

sys.argv.append('py2exe')  

py2exe_options = {  
    "includes": ["sip"],  # 如果打包檔案中有PyQt程式碼,則這句為必須新增的  
    "dll_excludes": ["MSVCP90.dll",],  # 這句必須有,不然打包後的程式執行時會報找不到SVCP90.dll,如果打包過程中找不到這個檔案,請安裝相應的庫  
    "compressed": 1,  
    "optimize": 2,  
    "ascii": 0,  
    "bundle_files": 1,  # 關於這個引數請看第三部分中的問題(2)  
}  
setup(
    name = "tianqi", 
    version = '1.0',  
    windows = ['weather_zhwnl.py',],   # 括號中更改為你要打包的程式碼檔名  
    zipfile = None,  
    options = {'py2exe': py2exe_options}  
) 

這裡面究竟差了點啥呢

我還試過PyInstaller 結果跟 py2exe類似,執行程式是彈出那個視窗。

相關文章