java打包python到exe文件
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                java打包python到exe文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            最近想把寫的python代碼打包,以供沒用安裝python環境的同事使用,需求如下:?
采用方案如下:?
使用py2exe自動導入類庫?
建立文件bin_setup.py?
Python代碼??#!/usr/bin/env?python?? #?-*-?coding:?utf-8?-*-?? ?? __author__?=?'lxd'?? ?? from?distutils.core?import?setup???? import?py2exe???? import?sys?? ?? #?If?run?without?args,?build?executables,?in?quiet?mode.?? if?len(sys.argv)?==?1:?? ????sys.argv.append("py2exe")?? ????sys.argv.append("-q")?? ?? INCLUDES?=?[]?? ?? MANIFEST_TEMPLATE?=?"""? <?xml?version="1.0"?encoding="UTF-8"?standalone="yes"?>? <assembly?xmlns="urn:schemas-microsoft-com:asm.v1"?manifestVersion="1.0">? ??<assemblyIdentity? ????version="5.0.0.0"? ????processorArchitecture="x86"? ????name="%(prog)s"? ????type="win32"? ??/>? ??<description>%(prog)s</description>? ??<trustInfo?xmlns="urn:schemas-microsoft-com:asm.v3">? ????<security>? ??????<requestedPrivileges>? ????????<requestedExecutionLevel? ????????????level="asInvoker"? ????????????uiAccess="false">? ????????</requestedExecutionLevel>? ??????</requestedPrivileges>? ????</security>? ??</trustInfo>? ??<dependency>? ????<dependentAssembly>? ??????<assemblyIdentity? ????????????type="win32"? ????????????name="Microsoft.VC90.CRT"? ????????????version="9.0.21022.8"? ????????????processorArchitecture="x86"? ????????????publicKeyToken="1fc8b3b9a1e18e3b">? ??????</assemblyIdentity>? ????</dependentAssembly>? ??</dependency>? ??<dependency>? ????<dependentAssembly>? ????????<assemblyIdentity? ????????????type="win32"? ????????????name="Microsoft.Windows.Common-Controls"? ????????????version="6.0.0.0"? ????????????processorArchitecture="X86"? ????????????publicKeyToken="6595b64144ccf1df"? ????????????language="*"? ????????/>? ????</dependentAssembly>? ??</dependency>? </assembly>? """?? RT_MANIFEST?=?24?? ?? options?=?{"py2exe"?:?? ????{"compressed"?:?1,?? ?????"optimize"?:?2,?? ?????"bundle_files"?:?2,?? ?????"includes"?:?INCLUDES,?? ?????"excludes"?:?["Tkinter",],?? ?????"dll_excludes":?[?"MSVCP90.dll",?"mswsock.dll",?"powrprof.dll"]?}}?? ?? windows?=?[{"script":?"bin.py",?? ??????"icon_resources":?[(1,?"bin.ico")],?? ??????"other_resources"?:?[(RT_MANIFEST,?1,?? ????????????????????????MANIFEST_TEMPLATE?%?dict(prog="MyAppName"))],?? ??????}]?? ?? setup(name?=?"MyApp",?? ??????version?=?"1.0",?? ??????description?=?"Description?of?the?app",?? ??????author?=?"Author?Name",?? ??????author_email?="author@project.com",?? ??????maintainer?=?"Maintainer?Name",?? ??????maintainer_email?=?"you@project.com",?? ??????license?=?"wxWindows?Licence",?? ??????url?=?"http://projecthomepage.com",?? ?? ??????data_files?=?["MSVCR90.dll",?"gdiplus.dll"],?? ??????#data_files=[("img",[r"d:\test\1.gif",r"d:\test\2.gif"]),("xml",[r"d:\test\1.xml",r"d:\test\2.xml"])])?? ??????#zipfile=None,?? ??????options?=?options,?? ??????windows?=?windows,?? ??????)??  
使用7-ZIP壓縮library,使用upx壓縮dll等文件?
建立腳本bin.cmd?
Java代碼??@echo?off?? ?? ::Set?personal?Path?to?the?Apps:?? set?PythonEXE=D:\Python26\python.exe?? set?SevenZipEXE="D:\Program?Files\7-ZIP\7z.exe"?? set?UpxEXE="D:\Program?Files\upx\upx.exe"?? ?? ::?Compress=1?-?Use?CompressFiles?? ::?Compress=0?-?Don't?CompressFiles?? set?Compress=1?? ?? if?not?exist?%~dpn0.py??????????call?:FileNotFound?%~dpn0.py?? if?not?exist?%PythonEXE%????????call?:FileNotFound?%PythonEXE%?? if?not?exist?%SevenZipEXE%??????call?:FileNotFound?%SevenZipEXE%?? if?not?exist?%UpxEXE%???????????call?:FileNotFound?%UpxEXE%?? ?? ::Compile?the?Python-Script?? %PythonEXE%?"%~dpn0_setup.py"?py2exe?? if?not?"%errorlevel%"=="0"?(?? ????????echo?Py2EXE?Error!?? ????????pause?? ????????goto:eof?? )?? ?? ::?Copy?the?Py2EXE?Results?to?the?SubDirectory?and?Clean?Py2EXE-Results?? rd?build?/s?/q?? xcopy?dist\*.*?"%~dpn0_EXE\"?/d?/y?? ::?I?use?xcopy?dist\*.*?"%~dpn0_EXE\"?/s?/d?/y?? ::?This?is?necessary?when?you?have?subdirectories?-?like?when?you?use?Tkinter?? rd?dist?/s?/q?? ?? if?"%Compress%"=="1"?call:CompressFiles?? echo.?? echo.?? echo?Done:?"%~dpn0_EXE\"?? echo.?? pause?? goto:eof?? ?? :CompressFiles?? ????????%SevenZipEXE%?-aoa?x?"%~dpn0_EXE\library.zip"?-o"%~dpn0_EXE\library\"?? ????????del?"%~dpn0_EXE\library.zip"?? ?? ????????cd?%~dpn0_EXE\library\?? ????????%SevenZipEXE%?a?-tzip?-mx9?"..\library.zip"?-r?? ????????cd..?? ????????rd?"%~dpn0_EXE\library"?/s?/q?? ?? ????????cd?%~dpn0_EXE\?? ????????%UpxEXE%?--best?*.*?? goto:eof?? ?? :FileNotFound?? ????????echo.?? ????????echo?Error,?File?not?found:?? ????????echo?[%1]?? ????????echo.?? ????????echo?Check?Path?in?%~nx0????? ????????echo.?? ????????pause?? ????????exit?? goto:eof??  
使用方法:?
直接運行bin.cmd,程序會自動調用bin_setup.py來查找需要的類庫,然后對類庫文件進行壓縮,生成的可執行文件在bin_EXE里。?
問題:?
我在打包的時候,出現錯誤“ImportError: MemoryLoadLibrary failed loading win32api.pyd”,用depends.exe查看其引用,然后多方搜索得知,其原因是py2exe錯誤的加載了mswsock.dll,powrprof.dll這兩個文件,因此將它們排除即可。?
Python代碼??"dll_excludes":?[?"MSVCP90.dll",?"mswsock.dll",?"powrprof.dll"]?}}??  
nsis生成安裝文件?
待續。。。?
                        
                        
                        - 無python環境也可執行
- 文件盡量少,不要太亂
- 程序體積盡量小
- 如果需要更新的話重復類庫不用更新
采用方案如下:?
- 使用py2exe自動導入類庫
- 使用7-ZIP壓縮library
- upx壓縮dll等文件
- nsis生成安裝文件
- 采用md5驗證的方式判別不用更新的類庫
使用py2exe自動導入類庫?
建立文件bin_setup.py?
Python代碼??
使用7-ZIP壓縮library,使用upx壓縮dll等文件?
建立腳本bin.cmd?
Java代碼??
使用方法:?
直接運行bin.cmd,程序會自動調用bin_setup.py來查找需要的類庫,然后對類庫文件進行壓縮,生成的可執行文件在bin_EXE里。?
問題:?
我在打包的時候,出現錯誤“ImportError: MemoryLoadLibrary failed loading win32api.pyd”,用depends.exe查看其引用,然后多方搜索得知,其原因是py2exe錯誤的加載了mswsock.dll,powrprof.dll這兩個文件,因此將它們排除即可。?
Python代碼??
nsis生成安裝文件?
待續。。。?
?
轉載于:https://www.cnblogs.com/jefree/p/4461836.html
總結
以上是生活随笔為你收集整理的java打包python到exe文件的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: mcgs组态软件中字体如果从左到右变化_
- 下一篇: Git中的日常使用 码云
