Pyinstaller的打包使用

2022-08-10 19:27:19 字數 2122 閱讀 1165

[toc]

### 1,pyinstaller的安裝  

執行`pip install pyinstaller`

### 2,

常用打包方式  

單一檔案,pyinstaller -f -i iconame.ico filename.py

非單一檔案,pyinstaller -d -i iconame.ico filename.py

3,外部檔案的引用,如果涉及外部檔案的使用,建議以非單一檔案形式打包,一方面可以增加使用的彈性,另一方面更好的管理與使用,避免單一檔案過大。

如果外部檔案較少,可以直接手動拷貝檔案到相應目錄,

如果外部檔案較多,可以修改

yourexename.spec(可以使用pyi-makespec yourexename.py命令生成)檔案,示例如下:

#

-*- mode: python ; coding: utf-8 -*-

block_cipher =none

a = analysis(['

justtest.py'],

pathex=[

'd:\\pyprj\\upload\\officepro\\venv\\lib\\site-packages'],

binaries=,

datas=,

hiddenimports=['

openpyxl'],

hookspath=,

runtime_hooks=,

excludes=,

win_no_prefer_redirects=false,

win_private_assemblies=false,

cipher=block_cipher,

noarchive=false)

a.datas+=[('

people.xlsx

','d:\\pyprj\\upload\\birthdayreminder\\people.xlsx

','data'),

('birthday.icon

','d:\\pyprj\\upload\\birthdayreminder\\birthday.icon

','data')]

pyz =pyz(a.pure, a.zipped_data,

cipher=block_cipher)

exe =exe(pyz,

a.scripts,

,exclude_binaries=true,

name='

justtest',

debug=false,

bootloader_ignore_signals=false,

strip=false,

upx=true,

console=true )

coll =collect(exe,

a.binaries,

a.zipfiles,

a.datas,

strip=false,

upx=true,

upx_exclude=,

name='

justtest

')

如果打包後,找不到引入的庫,可以在pathx中加入庫的絕對路徑(注意格式),然後在

hiddenimports中加入庫的名字即可。

外部檔案較多,想實現打包自動拷貝,參照示例加入相應的檔案路徑到datas中即可實現自動拷貝,元組中各項表示檔名,路徑,型別。

最後執行pyinstaller yourexename.spec 即可。

也可以不按照上述的寫法,直接在datas中新增外部資料,例如

datas=[('filename','.')] 元組分別表示檔名和當前路徑,

在程式中對檔案的引用如下,

path=os.path.dirname(os.path.abspath(__file__

))os.path.join(path,filename)

注意:非pyinstaller yourexename.spec命令方式打包,會重新生成新的.spec檔案,覆蓋掉之前的。

pyinstaller打包程式

python打包成exe檔案時,用的是pyinstaller 第一步安裝pyinstaller pip install pyinstaller第二步 pyinstaller f w i ico py其中 ico 是logo,py是你要打包的py檔案 我在打包時出現了struct.error unpa...

pyinstaller打包使用

pyinstaller manage.py 如果直接打包報錯,使用如下命令可以直接生成配置檔案 pyi makespec d manage.py生成的配置檔案格式如下 mode python coding utf 8 block cipher none a analysis impala etl.p...

pyinstaller 打包總結

pyinstaller 打包過程總結 安裝 pyinstaller 打包工具 pip3 install pyinstallerpyinstaller 打包命令不熟悉的小夥伴可點選這裡檢視喲 備註 以下涉及到的檔案名字main.py,請替換成自己的檔名 執行打包命令生成單獨的 exe 程式 pyins...