py2exe打包笔记
iTip虽然已经打包成功,但是有两点遗憾,一是未能打包成单个exe文件,打包目录下零散文件太多;二是打包后窗体xp样式丢失@_@。今天,终于~~终于解决了!
先记录一下我打包遇到的问题:
- ImportError: No module named sqlite
- LookupError: unknown encoding: ascii
- 如何打包成单个exe文件
- 打包后窗口xp样式丢失
以上问题都在setup.py文件中解决!给出我的setup.py文件代码:
01
# Requires wxPython. This sample demonstrates:
02
#
03
# - single file exe using wxPython as GUI.
04
05
from
distutils.core
import
setup
06
import
py2exe
07
import
sys
08
09
# If run without args, build executables, in quiet mode.
10
11
if
len
(sys.argv)
=
=
1
:
12
sys.argv.append(
"py2exe"
)
13
sys.argv.append(
"-q"
)
14
15
class
Target:
16
def
__init__(
self
,
*
*
kw):
17
self
.__dict__.update(kw)
18
# for the versioninfo resources
19
self
.version
=
"1.0.0"
20
self
.company_name
=
"www.xsmile.net"
21
self
.copyright
=
"xsmile @2008"
22
self
.name
=
"iTip"
23
24
################################################################
25
26
# A program using wxPython
27
# The manifest will be inserted as resource into iTip.exe. <span style="color: #0000ff;">This</span>
28
<span style
=
"color: #0000ff;"
># gives the controls the Windows XP appearance (
if
run on XP <img src
=
"http://www.xsmile.net/wp-includes/images/smilies/icon_wink.gif"
alt
=
";-)"
class
=
"wp-smiley"
> <
/
span>
29
#
30
# Another option would be to store it in a file named
31
# iTip.exe.manifest, and copy it with the data_files option into
32
# the dist-dir.
33
#
34
manifest_template
=
'''
35
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
36
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
37
<assemblyIdentity
38
version="5.0.0.0"
39
processorArchitecture="x86"
40
name="%(prog)s"
41
type="win32"
42
/>
43
<description>%(prog)s Program</description>
44
<dependency>
45
<dependentAssembly>
46
<assemblyIdentity
47
type="win32"
48
name="Microsoft.Windows.Common-Controls"
49
version="6.0.0.0"
50
processorArchitecture="X86"
51
publicKeyToken="6595b64144ccf1df"
52
language="*"
53
/>
54
</dependentAssembly>
55
</dependency>
56
</assembly>
57
58
'''
59
60
RT_MANIFEST
=
24
61
62
iTip
=
Target(
63
# used for the versioninfo resource
64
description
=
"A ENote Application"
,
65
# what to build
66
script
=
"iTip.py"
,
67
68
other_resources
=
[(RT_MANIFEST,
1
, manifest_template
%
dict
(prog
=
"iTip"
))],
69
icon_resources
=
[(
1
,
"iTip.ico"
)],
70
dest_base
=
"iTip"
)
71
72
################################################################
73
74
<span style
=
"color: #0000ff;"
>includes
=
[
"encodings"
,
"encodings.*"
,
"sqlalchemy.databases.sqlite"
]<
/
span>
75
setup(
76
options
=
{
"py2exe"
: {
"compressed"
:
1
,
77
"optimize"
:
2
,
78
"ascii"
:
1
,
79
<span style
=
"color: #0000ff;"
>
"includes"
:includes,<
/
span>
80
"bundle_files"
:
1
}},
81
zipfile
=
None
,
82
windows
=
[iTip],
83
)
84
85
############################################################
其中includes语句指定py2exe需要打包的模块,这里就解决了1、2问题。剩下来的3、4问题的解决也在stup.py里面啦^_^