Pyinstaller将python打包为EXE文件将python脚本打包为EXE文件有不少选择方案,本方案使用pyinstaller。第一步:当然是到python的官网下载python并安装。编写好python脚本并且运行正常。第二步:到下载最新版本的pyinstaller。本文写作时是pyinstaller2.0,支持python2.3~2.7版本。下载后解压到某个文件夹,如:c:\pyinstaller。第三步:pyinstaller依赖一些windows的组件,需要到下载相应python的版本,例如:python2.7的32位版:pywin32-218.win32-py2.7.exe第四步:进入pyinstaller目录,试运行看看是否安装正确。以下是运行屏幕:C:\cdc:\pyinstallerC:\pyinstallerc:\python27\pythonpyinstaller.pyError:PyInstallerforPython2.6+onWindowsneedspywin32.Pleaseinstallfrom上面的错误提示安装pywin32-218.win32-py2.7.exe。C:\pyinstallerc:\python27\pythonpyinstaller.pyUsage:pythonpyinstaller.py[opts]scriptname[scriptname...]|specfilepyinstaller.py:error:Requiresatleastonescriptnamefileorexactlyone.spec-file#以上信息表示可以开展工作了。#以下是测试一个python-tools.py文件的打包,文件放在当前目录的mytools文件夹里面C:\pyinstallerc:\python27\pythonpyinstaller.py--console--onefilemytools\python-tools.py67INFO:wroteC:\pyinstaller\python-tools\python-tools.spec175INFO:Testingforabilitytoseticons,versionresources...224INFO:...resourceupdateavailable234INFO:UPXisnotavailable.3179INFO:checkingAnalysis3179INFO:buildingAnalysisbecauseout00-Analysis.tocnonexistent3179INFO:runningAnalysisout00-Analysis.toc3189INFO:AddingMicrosoft.VC90.CRTtodependentassembliesoffinalexecutable3218INFO:Searchingforassemblyx86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww...3218INFO:FoundmanifestC:\WINDOWS\WinSxS\Manifests\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375.manifest3228INFO:Searchingforfilemsvcr90.dll3228INFO:FoundfileC:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\msvcr90.dll3228INFO:Searchingforfilemsvcp90.dll3228INFO:FoundfileC:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\msvcp90.dll3238INFO:Searchingforfilemsvcm90.dll3238INFO:FoundfileC:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\msvcm90.dll3648INFO:AnalyzingC:\pyinstaller\support\_pyi_bootstrap.py6486INFO:AnalyzingC:\pyinstaller\PyInstaller\loader\archive.py6855INFO:AnalyzingC:\pyinstaller\PyInstaller\loader\carchive.py7242INFO:AnalyzingC:\pyinstaller\PyInstaller\loader\iu.py7329INFO:Analyzingmytools\python-tools.py8355INFO:checkingTree8365INFO:buildingbecauseout00-Tree.tocmissingorbad8365INFO:buildingTreeout00-Tree.toc8894INFO:checkingTree8894INFO:buildingbecauseout01-Tree.tocmissingorbad8905INFO:buildingTreeout01-Tree.toc9304INFO:Hiddenimport'encodings'hasbeenfoundotherwise9315INFO:Lookingforrun-timehooks9315INFO:AnalyzingrthookC:\pyinstaller\support/rthooks/pyi_rth_encodings.py9585INFO:AnalyzingrthookC:\pyinstaller\support/rthooks/pyi_rth_Tkinter.py10569INFO:AddingMicrosoft.Windows.Common-Controlstodependentassembliesoffinalexecutable11896INFO:WarningswrittentoC:\pyinstaller\python-tools\build\pyi.win32\python-tools\warnpython-tools.txt11906INFO:checkingPYZ11906INFO:rebuildingout00-PYZ.tocbecauseout00-PYZ.pyzismissing11917INFO:buildingPYZout00-PYZ.toc13763INFO:checkingPKG13774INFO:rebuildingout00-PKG.tocbecauseout00-PKG.pkgismissing13774INFO:buildingPKGout00-PKG.pkg19944INFO:checkingEXE19944INFO:rebuildingout00-EXE.tocbecausepython-tools.exemissing19944INFO:buildingEXEfromout00-EXE.toc19974INFO:AppendingarchivetoEXEC:\pyinstaller\python-tools\dist\python-tools.exe#上面编译出来的exe能够正常运行了,但带一个黑色的console,以下重新编译,加入--windowed--icon,取消--consoleC:\pyinstallerc:\python27\pythonpyinstaller.py--windowed--onefile--icon=c:\pyinstaller\mytools\ico.icomytools\python-tools.py69INFO:wroteC:\pyinstaller\python-tools\python-tools.spec119INFO:Testingforabilitytoseticons,versionresources...139INFO:...resourceupdateavailable149INFO:UPXisnotavailable.2312INFO:checkingAnalysis2422INFO:checkingPYZ2462INFO:checkingPKG2542INFO:buildingbecauseC:\pyinstaller\python-tools\build\pyi.win32\python-tools\python-tools.exe.manifestchanged2542INFO:buildingPKGout00-PKG.pkg6468INFO:checkingEXE6478INFO:rebuildingout00-EXE.tocbecausepython-tools.exemissing6478INFO:buildingEXEfromout00-EXE.toc6488INFO:SRCPATH[('c:\\pyinstaller\\mytools\\ico.ico',None)]6488INFO:Updatingiconsfrom['c:\\pyinstaller\\mytools\\ico.ico']toc:\docume~1\user\locals~1\temp\tmpimsedq6498INFO:WritingRT_GROUP_ICON0resourcewith20bytes6498INFO:WritingRT_ICON1resourcewith2216bytes6528INFO:AppendingarchivetoEXEC:\pyinstaller\python-tools\dist\python-tools.exe#好了,大功告成。