将python源文件打包成exe文件

 

 1 D:\test>pyinstaller -F woqi.py
 2 160 INFO: PyInstaller: 3.2.1
 3 161 INFO: Python: 2.7.7
 4 161 INFO: Platform: Windows-7-6.1.7601-SP1
 5 164 INFO: wrote D:\test\woqi.spec
 6 174 INFO: UPX is not available.
 7 181 INFO: Extending PYTHONPATH with paths
 8 ['D:\\test', 'D:\\test']
 9 183 INFO: checking Analysis
10 184 INFO: Building Analysis because out00-Analysis.toc is non existent
11 184 INFO: Initializing module dependency graph...
12 188 INFO: Initializing module graph hooks...
13 292 INFO: running Analysis out00-Analysis.toc
14 296 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
15   required by D:\Python27\python.exe
16 388 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
17 1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
18 391 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
19 1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
20 392 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
21 1fc8b3b9a1e18e3b_9.0.30729.4940_none_acd19a1fe1da248a.manifest
22 394 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
23 1fc8b3b9a1e18e3b_9.0.30729.7523_none_accf5b75e1dc34f0.manifest
24 480 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.3
25 0729.7523_none ...
26 481 INFO: Found manifest C:\Windows\WinSxS\Manifests\amd64_microsoft.vc90.crt_1f
27 c8b3b9a1e18e3b_9.0.30729.7523_none_08e1eaf5a83f8ea2.manifest
28 483 INFO: Searching for file msvcr90.dll
29 484 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b
30 _9.0.30729.7523_none_08e1eaf5a83f8ea2\msvcr90.dll
31 486 INFO: Searching for file msvcp90.dll
32 486 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b
33 _9.0.30729.7523_none_08e1eaf5a83f8ea2\msvcp90.dll
34 486 INFO: Searching for file msvcm90.dll
35 487 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b
36 _9.0.30729.7523_none_08e1eaf5a83f8ea2\msvcm90.dll
37 575 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
38 1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
39 576 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
40 1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
41 578 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
42 1fc8b3b9a1e18e3b_9.0.30729.4940_none_acd19a1fe1da248a.manifest
43 581 INFO: Found C:\Windows\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_
44 1fc8b3b9a1e18e3b_9.0.30729.7523_none_accf5b75e1dc34f0.manifest
45 582 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0,
46 30729, 7523)
47 697 INFO: Caching module hooks...
48 703 INFO: Analyzing D:\test\woqi.py
49 2139 INFO: Processing pre-find module path hook   distutils
50 2315 INFO: Loading module hooks...
51 2316 INFO: Loading module hook "hook-distutils.py"...
52 2319 INFO: Loading module hook "hook-encodings.py"...
53 2948 INFO: Looking for ctypes DLLs
54 2950 INFO: Analyzing run-time hooks ...
55 2957 INFO: Looking for dynamic libraries
56 3125 INFO: Looking for eggs
57 3125 INFO: Using Python library C:\Windows\system32\python27.dll
58 3125 INFO: Found binding redirects:
59 [BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'amd64', oldVe
60 rsion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 7523), publicKeyToken=u'1fc8b3b
61 9a1e18e3b')]
62 3134 INFO: Warnings written to D:\test\build\woqi\warnwoqi.txt
63 3177 INFO: checking PYZ
64 3178 INFO: Building PYZ because out00-PYZ.toc is non existent
65 3180 INFO: Building PYZ (ZlibArchive) D:\test\build\woqi\out00-PYZ.pyz
66 3565 INFO: Building PYZ (ZlibArchive) D:\test\build\woqi\out00-PYZ.pyz completed
67  successfully.
68 3611 INFO: checking PKG
69 3611 INFO: Building PKG because out00-PKG.toc is non existent
70 3612 INFO: Building PKG (CArchive) out00-PKG.pkg
71 3647 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 307
72 29, 7523)
73 4856 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
74 4865 INFO: Bootloader D:\Python27\lib\site-packages\PyInstaller\bootloader\Windo
75 ws-64bit\run.exe
76 4866 INFO: checking EXE
77 4868 INFO: Building EXE because out00-EXE.toc is non existent
78 4868 INFO: Building EXE from out00-EXE.toc
79 4869 INFO: Appending archive to EXE D:\test\dist\woqi.exe
80 4924 INFO: Building EXE from out00-EXE.toc completed successfully.
View Code

 

1.通过-F 参数对 Python 源文件生成一个独立的可执行文件,否则是一个带各种dll和依赖文件的文件夹

2.在哪个目录下打包,这些文件和exe最后就生成在哪里

源码在里面,在此目录下运行打包命令,打包后的文件就生成在此目录

 

 

 

打包命令-pyinstaller.exe

参数:
-F, –onefile 打包成一个exe文件。
-D, –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)。
-c, –console, –nowindowed 使用控制台,无界面(默认)
-w, –windowed, –noconsole 使用窗口,无控制台

一般成功了,会在打包的目录下生成build和dist文件夹及.spec文件(如:139MailLogin.spec)

在cmd窗口,执行打包命令
打包目录(建议在源代码所在路径)>pyinstaller.exe -F 文件路径(打包目录和源代码目录不一致时,需要用绝对路径)

 

 

3.打包后的.exe可执行程序(只复制.exe文件就可以,其他的.spec,dist,bulid文件或目录都可以删了)在其他电脑上运行,不需要该电脑有Python环境

4.打包的代码里面如果中文没处理好会导致打包不成功,即使成功运行也会闪退

pyinstaller 打包的exe SyntaxError: (unicode error) 'ascii' codec can't decode byte 0xe8 in position 0:  ordinal not in range(128)

 

实例:

随机生成或让用户输入条件生成对应的制卡模拟卡数据和索引文件

源码:https://github.com/YuanrangChan/woqi-wcpcarddata(打包时注意中文处理)

 

打包命令:

C:\Users\Administrator\Desktop\test>pyinstaller.exe -F C:\Users\Administrator\Desktop\test\RandCarddataIndexWoqi.py

打包后的文件:自己按照上述命令打包即可生成

 

参考:

python学习笔记--将python源文件打包成exe文件 - CSDN博客  https://blog.csdn.net/u010812071/article/details/78507946

Python | 用Pyinstaller打包发布exe应用_百度经验  https://jingyan.baidu.com/article/a378c960b47034b3282830bb.html

关于python打包成exe的一点经验之谈 - CSDN博客  https://blog.csdn.net/ztsghjls/article/details/54426169

posted @ 2018-08-05 22:01  明媚如你  阅读(12769)  评论(0编辑  收藏  举报