- 安裝
pyinstaller
cmd
管理者模式安裝
C:\OSGeo4W\bin\python-qgis-ltr.bat -m pip install pyinstaller
-
測試安裝
指令行運作
如圖所示安裝成功。C:\OSGeo4W\bin\python-qgis-ltr.bat -m PyInstaller
- 編寫示例程式
install.py
import sys,os
from qgis.core import QgsApplication
from qgis.gui import QgsMapCanvas
os.environ['GDAL_DATA'] = r'C:\OSGeo4W\share\gdal'
os.environ['PROJ_LIB'] = r'C:\OSGeo4W\share\proj'
def init():
a = QgsApplication([], True)
QgsApplication.setPrefixPath('qgis', True)
QgsApplication.initQgis()
return a
def show_canvas(app):
canvas = QgsMapCanvas()
canvas.show()
app.exec_()
app = init()
show_canvas(app)
指令行運作
C:\OSGeo4W\bin\python-qgis-ltr.bat install.py
如下圖所示:
- 編寫打包
install.bat
"C:\OSGeo4W\bin\python-qgis-ltr.bat" -m PyInstaller -w ^
--icon=logo.ico ^
--add-data="C:\OSGeo4W\apps\qgis-ltr\plugins;qgis\plugins" ^
--add-data="C:\OSGeo4W\apps\Python37\Lib\site-packages\PyQt5\*.pyd;PyQt5" ^
--add-data="C:\OSGeo4W\apps\qt5\plugins\styles;PyQt5\Qt\plugins\styles" ^
--add-data="C:\OSGeo4W\apps\qt5\plugins\iconengines;PyQt5\Qt\plugins\iconengines" ^
--add-data="C:\OSGeo4W\apps\qt5\plugins\imageformats;PyQt5\Qt\plugins\imageformats" ^
--add-data="C:\OSGeo4W\apps\qt5\plugins\platforms;PyQt5\Qt\plugins\platforms" ^
--add-data="C:\OSGeo4W\apps\qt5\plugins\platformthemes;PyQt5\Qt\plugins\platformthemes" ^
install.py
解析:
C:\OSGeo4W\apps\qgis-ltr\plugins -> qgis\plugins
打包時将相應的檔案拷貝到指定的檔案夾下。
指令行在目前目錄下執行
install.bat
執行結果如下:
- 運作打包後的程式
執行結果:dist\install\install.exe
注意:
QgsApplication.setPrefixPath('qgis', True)
指的是打包後的安裝目錄。
Reference
https://blog.csdn.net/this_is_id/article/details/102974721