Pyinstaller 轉exe 並添加其他附件
Pyinstaller 轉 EXE很簡單
但是要打包其他附件
像是selenium 的chromedriver.exe 或 geckodriver.exe、mp3、jpg、png等等
一、資料夾結構
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
然後程式有用到的路徑都套用這個函式
例如:
resource_path(r"doc_system\a.exe")
#紅字是你要手動加在你的SPEC檔當中
# -*- mode: python -*-
block_cipher = None
added_files = [
( 'doc_system/*.exe', 'doc_system' ),
( 'doc_system/*.bat', 'doc_system' ),
( 'doc_system/*.ini', 'doc_system' ),
]
a = Analysis(['main.py'],
pathex=['C:\\Users\\user\\Desktop\\doc'],
binaries=[],
datas= added_files,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
extra_tree = Tree('./doc_system', prefix = 'doc_system')
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='main',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False )
但是要打包其他附件
像是selenium 的chromedriver.exe 或 geckodriver.exe、mp3、jpg、png等等
一、資料夾結構
/doc
|----/
doc_system
|----|----a.exe
|----|----b.bat
|----|----c.ini
|----main.py
一、在你的程式碼中最上方加上這段:
import osdef resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
然後程式有用到的路徑都套用這個函式
例如:
resource_path(r"doc_system\a.exe")
二、再來要產生spec檔
#紅字是你要手動加在你的SPEC檔當中
# -*- mode: python -*-
block_cipher = None
added_files = [
( 'doc_system/*.exe', 'doc_system' ),
( 'doc_system/*.bat', 'doc_system' ),
( 'doc_system/*.ini', 'doc_system' ),
]
a = Analysis(['main.py'],
pathex=['C:\\Users\\user\\Desktop\\doc'],
binaries=[],
datas= added_files,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
extra_tree = Tree('./doc_system', prefix = 'doc_system')
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='main',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False )
留言
張貼留言