Pyinstaller將Python程序打包成EXE(多種模式的打包)

Pyinstaller將Python程序打包成EXE

安裝

pip install pyinstaller
複製代碼

總體流程

pyi-makespec xxx.py     # 先生成spec文件
pyinstaller xxx.spec    # 再生成exe文件
複製代碼

參數說明

-F :打包成一個exe文件(在dist文件夾下)git

pyi-makespec -F xxx.py
pyinstaller xxx.spec
複製代碼

-D :生成一個包含exe的文件夾github

pyi-makespec -D xxx.py
pyinstaller xxx.spec
複製代碼

單一PY文件

按上述操做便可bash

包含數據文件

用文本編輯器打開spec文件,修改文件中的參數 datas=[]app

例如要打包 global_popu.tif 到exe中時編輯器

  1. pec文件中的參數修改成datas=[('global_popu.tif', '.')]
  2. 而後將spec文件編譯成exe,兩點注意:
    1. 將數據文件和xxx.py文件放在同一目錄
    2. 對xxx.py程序讀取數據的部分須要修改,修改方法以下
a = Analysis(['trip_popu.py'],
             pathex=['E:\\GitHub\\Zone\\Zone'],
             binaries=[],
             datas=[('global_popu.tif', '.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
複製代碼
# 對xxx.py的修改
# 添加自定義的函數
def resource_path(relative_path):    
	base_path = getattr(        
		sys, '_MEIPASS', os.path.dirname(            
			os.path.abspath(__file__)))    
	return os.path.join(base_path, relative_path)
	
#global_popu_Dic = resource_path("global_popu.tif") # 打包時使用
global_popu_Dic = "../data/global_popu.tif"			# 調試時使用
複製代碼

若是要打包兩個或者多個數據文件,則對spec中的datas 修改參考下例:函數

datas=[('china_light_2016.tif', '.'),('china_population_2016.tif', '.')]ui

多個PY文件

總體操做與上述相似,關鍵在於修改spec中的參數spa

例如:code文件夾下的trip_popu.py用到了zone_func文件夾目錄下的func.py文件,.net

在spec文件中的Analysis的第一個參數中加上func.py文件的絕對路徑,而後生成exe便可調試

a = Analysis(['trip_popu.py','E:\\GitHub\\Zone\\Zone\\zone_func\\func.py'],
             pathex=['E:\\GitHub\\Zone\\Zone'],
             binaries=[],
             datas=[('global_popu.tif', '.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
複製代碼

多進程打包

編寫的程序中包含多進程處理時,正常打包運行exe後,電腦會卡死

因此在打包前,需先要對xxx.py文件進行修改,修改內容爲:

import multiprocessing

if __name__ == "__main__":
    multiprocessing.freeze_support()	# 這一句必定要放在if __name__ == "__main__":下面
複製代碼

修改後,正常打包便可

問題記錄

  1. 程序有使用到shapely庫,打包時會提示缺乏geos.dll

    將提示的shapely庫的路徑下的geos_c.dll複製一份重命名爲geos.dll,從新打包便可

  2. 程序有使用到geopandas庫,打包時運行後提示

    File "site-packages\geopandas\datasets\__init__.py", line 7, in <module>
    StopIteration
    [6764] Failed to execute script application
    複製代碼

    找到geopandas庫文件下的__init__.py,將import geopandas.datasets這句註釋掉

  3. 程序有使用到sklearn庫時,打包運行時提示缺乏一些模塊,如sklearn.utils._cython_blas

    修改spec文件中的hiddenimports參數後,從新打包,修改以下

    a = Analysis(['mainarea_buffer.py','E:\\GitHub\\Zone\\Zone\\zone_func\\func.py'],
                 pathex=['E:\\GitHub\\Zone\\Zone'],
                 binaries=[],
                 datas=[('global_popu.tif', '.')],
                 hiddenimports=['sklearn.utils._cython_blas','cython', 'sklearn', 'sklearn.ensemble','sklearn.neighbors.typedefs','sklearn.neighbors.quad_tree','sklearn.tree._utils','scipy._lib.messagestream'],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher,
                 noarchive=False)
    
    複製代碼

參考

Github : github.com/guangmujun

CSDN : me.csdn.net/qq_37054356

相關文章
相關標籤/搜索