我们知道,Python很优雅,很值得学习。但是Python是解释性语言,代码需要有Python解释器才能执行,相比较我们平时直接运行exe等可执行文件多了一步的麻烦。
于是,希望能将Python程序打包成exe,使用起来多方便。
PyInstaller可以将Python程序打包成Windows(当然也包括Linux, Mac OS X, Solaris and AIX)下可执行的EXE文件,目前支持python 2.7 and 3.3—3.6版本。
官网:http://www.pyinstaller.org/
使用pip命令安装:pip install PyInstaller(会将依赖Pywin32也安装,方便)
安装后看下pyinstaller的版本: pyinstaller --version
下面开始使用pyinstaller打包程序:python2.7+pyinstaller3.3
1、新建py文件: pyInstaller.py
pyInstaller.py
1
2 3 4 5
|
# pyinstaller
# pip install pyinstaller print "Hello Python!" msg = input()
|
2、执行打包:pyinstaller pyInstaller.py
常用选项参数:pyinstaller -h 获取详细信息
-F 表示生成单个可执行文件
-w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!
-p 表示你自己自定义需要加载的类路径,一般情况下用不到
-i 表示可执行文件的图标
pyinstaller pyInstaller.py
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
C:Python27src>pyinstaller pyInstaller.py
60 INFO: PyInstaller: 3.3.1 60 INFO: Python: 2.7.15 60 INFO: Platform: Windows-7-6.1.7601-SP1 61 INFO: wrote C:Python27srcpyInstaller.spec 62 INFO: UPX is not available. 65 INFO: Extending PYTHONPATH with paths ['C:\Python27\src', 'C:\Python27\src'] 68 INFO: checking Analysis 68 INFO: Building Analysis because out00-Analysis.toc is non existent 69 INFO: Initializing module dependency graph... 71 INFO: Initializing module graph hooks... 156 INFO: running Analysis out00-Analysis.toc 189 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable required by c:python27python.exe 1780 INFO: Found C:WindowsWinSxSManifestsx86_policy.9.0.microsoft.vc90.crt_1 fc8b3b9a1e18e3b_9.0.30729.1_none_8550c6b5d18a9128.manifest 1782 INFO: Found C:WindowsWinSxSManifestsx86_policy.9.0.microsoft.vc90.crt_1 fc8b3b9a1e18e3b_9.0.30729.4940_none_f47ed0f6f6564d90.manifest 1791 INFO: Found C:WindowsWinSxSManifestsx86_policy.9.0.microsoft.vc90.crt_1 fc8b3b9a1e18e3b_9.0.30729.6161_none_f480bfaef65491a5.manifest 1823 INFO: Searching for assembly x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30 729.6161_none ... 1824 INFO: Found manifest C:WindowsWinSxSManifestsx86_microsoft.vc90.crt_1fc 8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57.manifest 1825 INFO: Searching for file msvcr90.dll 1825 INFO: Found file C:WindowsWinSxSx86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_ 9.0.30729.6161_none_50934f2ebcb7eb57msvcr90.dll 1825 INFO: Searching for file msvcp90.dll 1825 INFO: Found file C:WindowsWinSxSx86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_ 9.0.30729.6161_none_50934f2ebcb7eb57msvcp90.dll 1825 INFO: Searching for file msvcm90.dll 1827 INFO: Found file C:WindowsWinSxSx86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_ 9.0.30729.6161_none_50934f2ebcb7eb57msvcm90.dll 1856 INFO: Found C:WindowsWinSxSManifestsx86_policy.9.0.microsoft.vc90.crt_1 fc8b3b9a1e18e3b_9.0.30729.1_none_8550c6b5d18a9128.manifest 1857 INFO: Found C:WindowsWinSxSManifestsx86_policy.9.0.microsoft.vc90.crt_1 fc8b3b9a1e18e3b_9.0.30729.4940_none_f47ed0f6f6564d90.manifest 1858 INFO: Found C:WindowsWinSxSManifestsx86_policy.9.0.microsoft.vc90.crt_1 fc8b3b9a1e18e3b_9.0.30729.6161_none_f480bfaef65491a5.manifest 1859 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 6161) 2138 INFO: Caching module hooks... 2144 INFO: Analyzing C:Python27srcpyInstaller.py 2144 INFO: Loading module hooks... 2146 INFO: Loading module hook "hook-encodings.py"... 5555 INFO: Looking for ctypes DLLs 5555 INFO: Analyzing run-time hooks ... 5561 INFO: Looking for dynamic libraries 5680 INFO: Looking for eggs 5682 INFO: Using Python library C:Windowssystem32python27.dll 5682 INFO: Found binding redirects: [BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'x86', oldVers ion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 6161), publicKeyToken=u'1fc8b3b9a 1e18e3b')] 5685 INFO: Warnings written to C:Python27srcuildpyInstallerwarnpyInstaller .txt 5711 INFO: Graph cross-reference written to C:Python27srcuildpyInstallerxr ef-pyInstaller.html 5748 INFO: checking PYZ 5748 INFO: Building PYZ because out00-PYZ.toc is non existent 5749 INFO: Building PYZ (ZlibArchive) C:Python27srcuildpyInstallerout00-PY Z.pyz 5993 INFO: Building PYZ (ZlibArchive) C:Python27srcuildpyInstallerout00-PY Z.pyz completed successfully. 6028 INFO: checking PKG 6029 INFO: Building PKG because out00-PKG.toc is non existent 6029 INFO: Building PKG (CArchive) out00-PKG.pkg 6048 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 6051 INFO: Bootloader c:python27libsite-packagesPyInstallerootloaderWindo ws-32bit un.exe 6051 INFO: checking EXE 6051 INFO: Building EXE because out00-EXE.toc is non existent 6052 INFO: Building EXE from out00-EXE.toc 6052 INFO: Appending archive to EXE C:Python27srcuildpyInstallerpyInstalle r.exe 6054 INFO: Building EXE from out00-EXE.toc completed successfully. 6060 INFO: checking COLLECT 6061 INFO: Building COLLECT because out00-COLLECT.toc is non existent 6063 INFO: Building COLLECT out00-COLLECT.toc 6073 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 307 29, 6161) 6291 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 307 29, 6161) 6297 INFO: Updating manifest in C:UsersMichaelAppDataRoamingpyinstallerin cache00_py27_32bitpython27.dll 6382 INFO: Updating resource type 24 name 2 language 1033 6572 INFO: Building COLLECT out00-COLLECT.toc completed successfully. |
执行完毕后,会在当前目录的dist 中生成pyInstaller.exe