php mysql-net user
2023年3月31日发(作者:音频调节器)
【解决⽅案】Pyinstaller打包exe⽂件详细教程
在业务场景中,经常需要Python开发⼀些⼩程序/脚本/GUI界⾯,交付给⼀些⼩⽩或未安装Python的⼩伙伴们使⽤。
噔噔蹬蹬
pyinstaller闪亮登场
场景说明
使⽤Pyinstaller将Python脚本或者项⽬打包,⽣成可执⾏的.exe⽂件。
Pyinstaller是⼀个很不错的免费打包⼯具,⽀持Windows,Linux和MacOS,完美⽀持32位和64位系统。
除了Pyinstaller,还可以使⽤py2exe进⾏打包
pyinstaller安装
pipinstallpyinstaller
打包参数
#常⽤打包参数
#-F-D互斥参数
-F打包成⼀个exe⽂件,⼩项⽬可以采⽤,打开⽐较慢,多个.py打包时不能使⽤
-D默认参数,打包结果放⼊到创建的⽂件夹中,可以看到⾥⾯有很多的依赖⽂件
#-w-c互斥参数
-w使⽤项⽬的GUI界⾯,⽆cmd控制台
-c默认参数,使⽤cmd控制台如果打包⽂件执⾏报错,可尝试⽤-c显⽰控制台
-n执⾏项⽬的名称,默认.py的⽂件名
-i将ico图标打包到exe⽂件中
--hidden-import打包时导包信息
#打包PyQt项⽬强烈建议带上以下参数
--
#打包命令⽰例
#在项⽬的根⽬录下执⾏打包命令
#打包结果输出在项⽬根⽬录下的dist⽂件夹中
#不建议使⽤-F打包成⼀个exe⽂件所谓:打包⼀时爽,打开5秒钟
#⾮-F命令下,静态资源、建好的Sqlite数据库可以直接放⼊dist中⽣成的⽂件夹中
说明
aller只是将Python解释器和脚本打包成⼀个可执⾏⽂件,不同于编译成真正的机器码。所以代码的运⾏效率是降低的,只是
解决了⼩⽩⽤户的操作难度。
2.打包时会分析项⽬的依赖,并将相关的依赖打包进来,并进⾏加密处理
3.不同的操作系统,需要在不同的操作系统进⾏打包。
打包问题解决⽅法汇总
debug
控制台debug
在本地运⾏成功,建议在第⼀次打包exe时使⽤-c命令,这样可以在控制台中显⽰可能的报错信息。
如果打包时没有⽤-c不会出现控制台或者控制台⼀闪⽽过,看不到报错信息,可以在cmd命令⾏窗⼝执⾏exe⽂件
fatalerrorfaildtoexecutescriptfile
Pyinstaller打包时有些库没有打包进去,可以将报错信息中的库通过--hidden-import报错的包的进⾏打包
依赖报错
打包结果⽂件过⼤
帮助中的参数
>>>pyinstaller-h
usage:pyinstaller[-h][-v][-D][-F][--specpathDIR][-nNAME]
[--add-data
[--add-binary
[--hidden-importMODULENAME]
[--additional-hooks-dirHOOKSPATH]
[--runtime-hookRUNTIME_HOOKS][--exclude-moduleEXCLUDES]
[--keyKEY][-d{all,imports,bootloader,noarchive}][-s]
[--noupx][--upx-excludeFILE][-c][-w]
[-i<,>]
[--version-fileFILE][-m
[--uac-admin][--uac-uiaccess][--win-private-assemblies]
[--win-no-prefer-redirects]
[--osx-bundle-identifierBUNDLE_IDENTIFIER]
[--runtime-tmpdirPATH][--bootloader-ignore-signals]
[--distpathDIR][--workpathWORKPATH][-y]
[--upx-dirUPX_DIR][-a][--clean][--log-levelLEVEL]
scriptname[scriptname...]
positionalarguments:
scriptnamenameofscriptfilestobeprocessedorexactlyone
.-fileisspecified,mostoptions
areunnecessaryandareignored.
optionalarguments:
-h,--helpshowthishelpmessageandexit
-v,--versionShowprogramversioninfoandexit.
--distpathDIRWheretoputthebundledapp(default:.dist)
--workpathWORKPATHWheretoputallthetemporaryworkfiles,.log,.pyz
andetc.(default:.build)
-y,--noconfirmReplaceoutputdirectory(default:
SPECPATHdistSPECNAME)withoutaskingfor
confirmation
--upx-dirUPX_DIRPathtoUPXutility(default:searchtheexecution
path)
-a,--asciiDonotincludeunicodeencodingsupport(default:
includedifavailable)
--cleanCleanPyInstallercacheandremovetemporaryfiles
beforebuilding.
--
maybeoneofTRACE,DEBUG,INFO,WARN,ERROR,
CRITICAL(default:INFO).
Whattogenerate:
-D,--onedirCreateaone-folderbundlecontaininganexecutable
(default)
-F,--onefileCreateaone-filebundledexecutable.
--specpathDIRFoldertostorethegeneratedspecfile(default:
currentdirectory)
-nNAME,--nameNAMENametoassigntothebundledappandspecfile
(default:firstscript'sbasename)
Whattobundle,wheretosearch:
--add-data
Additionalnon-binaryfilesorfolderstobeaddedto
hseparatorisplatform
specific,``p``(whichis``;``onWindows
and``:``onmostunixsystems)tion
canbeusedmultipletimes.
--add-binary
Additionalbinaryfilestobeaddedtotheexecutable.
Seethe``--add-data``
optioncanbeusedmultipletimes.
-pDIR,--pathsDIRApathtosearchforimports(likeusingPYTHONPATH).
Multiplepathsareallowed,separatedby';',oruse
thisoptionmultipletimes
--hidden-importMODULENAME,--hiddenimportMODULENAME
Nameanimportnotvisibleinthecodeofthe
script(s).Thisoptioncanbeusedmultipletimes.
--additional-hooks-dirHOOKSPATH
tion
canbeusedmultipletimes.
--runtime-hookRUNTIME_HOOKS
mehookis
codethatisbundledwiththeexecutableandis
executedbeforeanyothercodeormoduletosetup
optioncanbeusedmultipletimes.
--exclude-moduleEXCLUDES
Optionalmoduleorpackage(thePythonname,notthe
pathname)thatwillbeignored(asthoughitwasnot
found).Thisoptioncanbeusedmultipletimes.
--keyKEYThekeyusedtoencryptPythonbytecode.
Howtogenerate:
-d{all,imports,bootloader,noarchive},--debug{all,imports,bootloader,noarchive}
Provideassistancewithdebuggingafrozen
gumentmaybeprovidedmultiple
timestoselectseveralofthefollowingoptions.
-all:Allthreeofthefollowingoptions.
-imports:specifythe-voptiontotheunderlying
Pythoninterpreter,causingittoprintamessage
eachtimeamoduleisinitialized,showingthe
place(filenameorbuilt-inmodule)fromwhichit
/3/using/#id4.
-bootloader:tellthebootloadertoissueprogress
messageswhileinitializingandstartingthe
diagnoseproblemswith
missingimports.
-noarchive:insteadofstoringallfrozenPython
sourcefilesasanarchiveinsidetheresulting
executable,storethemasfilesintheresulting
outputdirectory.
-s,--stripApplyasymbol-tablestriptotheexecutableand
sharedlibs(notrecommendedforWindows)
--noupxDonotuseUPXevenifitisavailable(works
differentlybetweenWindowsand*nix)
--upx-excludeFILEPreventabinaryfrombeingcompressedwhenusingupx.
Thisistypicallyusedifupxcorruptscertain
thefilenameof
tioncanbeused
multipletimes.
WindowsandMacOSXspecificoptions:
-c,--console,--nowindowed
Openaconsolewindowforstandardi/o(default).On
Windowsthisoptionwillhavenoeffectifthefirst
scriptisa'.pyw'file.
-w,--windowed,--noconsole
WindowsandMacOSX:donotprovideaconsolewindow
forstandardi/SXthisalsotriggers
owsthisoption
willbesetifthefirstscriptisa'.pyw'
optionisignoredin*NIXsystems.
-i<,>,--icon<,>
-i<,>,--icon<,>
:applythaticontoaWindowsexecutable.
,ID,extracttheiconwithIDfromanexe.
:dleonMacOS
X
Windowsspecificoptions:
--version-fileFILEaddaversionresourcefromFILEtotheexe
-m
addmanifestFILEorXMLtotheexe
-rRESOURCE,--resourceRESOURCE
RESOURCEisonetofouritems,
FILE[,TYPE[,NAME[,LANGUAGE]]].FILEcanbeadatafile
oranexe/afiles,atleastTYPEandNAME
GEdefaultsto0ormaybe
specifiedaswildcard*toupdateallresourcesofthe
/dllfiles,allresources
fromFILEwillbeadded/updatedtothefinal
executableifTYPE,NAMEandLANGUAGEareomittedor
specifiedaswildcard*.Thisoptioncanbeused
multipletimes.
--uac-adminUsingthisoptioncreatesaManifestwhichwill
requestelevationuponapplicationrestart.
--uac-uiaccessUsingthisoptionallowsanelevatedapplicationto
workwithRemoteDesktop.
WindowsSide-by-sideAssemblysearchingoptions(advanced):
--win-private-assemblies
AnySharedAssembliesbundledintotheapplication
ans
theexactversionsoftheseassemblieswillalwaysbe
used,andanynewerversionsinstalledonuser
machinesatthesystemlevelwillbeignored.
--win-no-prefer-redirects
WhilesearchingforSharedorPrivateAssembliesto
bundleintotheapplication,PyInstallerwillprefer
nottofollowpoliciesthatredirecttonewer
versions,andwilltrytobundletheexactversionsof
theassembly.
MacOSXspecificoptions:
--osx-bundle-identifierBUNDLE_IDENTIFIER
dleidentifierisusedasthedefault
usualformisahierarchicalnameinreverseDNS
mple:
e(default:first
script'sbasename)
Rarelyusedspecialoptions:
--runtime-tmpdirPATH
Wheretoextractlibrariesandsupportfilesin
`onefile`-optionisgiven,the
bootloaderwillignoreanytemp-folderlocation
``_MEIxxxxxx``-folder
usethisoptiononlyif
youknowwhatyouaredoing.
--bootloader-ignore-signals
Tellthebootloadertoignoresignalsratherthan
in
visorprocesssignals
boththebootloaderandchild(ocess
group)toavoidsignallingthechildtwice.
更多推荐
platform exe
发布评论