程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] pyinstaller uses --add data --add binary to package additional resources

編輯:Python

Why use --add-data

There are many file formats in the program :

Source code .py
Image format .png .jpg .ico etc.
The configuration file .ini .json .xml etc.
Other executable files .bin .exe etc.
Model file .pth etc.
documentation .txt .md etc.
Be careful :

except .py outside , Other formats will not compile .
except .py outside , If you want to package other formats , Need to use --add-data Handle , Or manually copy ( It's too much trouble , You can always remember ?)

How to use --add-data?

usage :pyinstaller x.py --add-data=" source address ; Destination address ". windows With ; Division ,linux With : Division

for example : take config All files in the directory are packaged into the destination config Folder ( Does not exist will automatically create ) Next

 

pyinstaller x.py --add-data ".\\config\\*;.\\config"

Can be used multiple times --add-data

for example :

pyinstaller x.py  -n Demo2.0.3 --key [email protected])v -i "res\logo.ico"  
--add-data=".\*.txt;." --add-data=".\*.json;." --add-data="res\*.*;.\res" 
--add-data="dist\models\*.*;.\models"

.spec How to use in documents --add-data

# -*- mode: python ; coding: utf-8 -*-
 
block_cipher = pyi_crypto.PyiBlockCipher(key='[email protected])v')
 
 
a = Analysis(['x.py'],
             pathex=['D:\\Miniconda3\\envs\\cuda11\\Lib\\site-packages', 'D:\\project\\demo'],
             binaries=[],
             datas=[('.\\*.json', '.'), ('res\\*.*', '.\\res'), ('dist\\models\\*.*', '.\\models')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='Demo2.0.3',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False , icon='res\\logo.ico')
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='Demo2.0.3')

Analysis Inside datas Is the use of --add-data Corresponding place !

One [] in , There are many. (), Every () Separated by commas , On the left is the program path , On the right is the path into the package !

Write the full path for the deep path !

binaries=[('onnxruntime\\capi\\*.*','\\onnxruntime\\capi')],

--add-binary Same as --add-data similar


————————————————
Thanks to:https://blog.csdn.net/u012219045/article/details/114841287


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved