首页 > 学技术 > 技术网文 > Python > 正文

[原创] py2exe初接触(一)


来源 chinaunix.net 酷勤网整理

py2exe是什么?
py2exe是一种python发布工具,可以把python脚本转换成windows下的可执行程序,不需要安装python便可运行。
py2exe现在可以用来创建使用了wxPython, Tkinter, Pmw, PyGTK, pygame, win32com client and server 等模块的程序。

详细介绍可以看它的官方网站 http://starship.python.net/crew/theller/py2exe/

1. 下载安装py2exe
py2exe目前的版本是0.5.4,根据你安装的python的版本选择下载的文件
[u][url=http://prdownloads.sourceforge.net/py2exe/py2exe-0.5.4.win32-py2.3.exe?download]py2exe-0.5.4.win32-py2.3.exe[/u]

[u][url=http://prdownloads.sourceforge.net/py2exe/py2exe-0.5.4.win32-py2.4.exe?download]py2exe-0.5.4.win32-py2.4.exe[/u]

安装后的文件应该在你的python安装目录下的Lib\site-packages\py2exe

2.  使用py2exe
我们先准备一个简单的python程序hello.py

# hello.py
def main():
    print "Hello, World!"

if __name__ == '__main__':
   main() 
 


然后为使用py2exe写一个脚本setup.py

# setup.py
from distutils.core import setup
import py2exe
      
setup(console=["hello.py"])


运行setup.py,记得要传一个参数给它
python setup.py py2exe


应该看到一些输出信息
引用:running py2exe
creating E:\Projects\WorkSpace\Python\build
creating E:\Projects\WorkSpace\Python\build\bdist.win32
creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe
creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\collect
creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\temp
creating E:\Projects\WorkSpace\Python\dist
*** searching for required modules ***
*** parsing results ***
creating python loader for extension '_sre'
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
byte-compiling C:\Python23\lib\copy_reg.py to copy_reg.pyc
byte-compiling C:\Python23\lib\sre_compile.py to sre_compile.pyc
byte-compiling E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\temp\_sre.py to _sre.pyc
byte-compiling C:\Python23\lib\macpath.py to macpath.pyc
byte-compiling C:\Python23\lib\popen2.py to popen2.pyc
byte-compiling C:\Python23\lib\atexit.py to atexit.pyc
byte-compiling C:\Python23\lib\os2emxpath.py to os2emxpath.pyc
byte-compiling C:\Python23\lib\sre_constants.py to sre_constants.pyc
byte-compiling C:\Python23\lib\re.py to re.pyc
byte-compiling C:\Python23\lib\ntpath.py to ntpath.pyc
byte-compiling C:\Python23\lib\stat.py to stat.pyc
byte-compiling C:\Python23\lib\string.py to string.pyc
byte-compiling C:\Python23\lib\warnings.py to warnings.pyc
byte-compiling C:\Python23\lib\UserDict.py to UserDict.pyc
byte-compiling C:\Python23\lib\repr.py to repr.pyc
byte-compiling C:\Python23\lib\copy.py to copy.pyc
byte-compiling C:\Python23\lib\types.py to types.pyc
byte-compiling C:\Python23\lib\posixpath.py to posixpath.pyc
byte-compiling C:\Python23\lib\sre.py to sre.pyc
byte-compiling C:\Python23\lib\linecache.py to linecache.pyc
byte-compiling C:\Python23\lib\sre_parse.py to sre_parse.pyc
byte-compiling C:\Python23\lib\os.py to os.pyc
*** copy extensions ***
copying C:\Python23\DLLs\_sre.pyd ->; E:\Projects\WorkSpace\Python\dist
*** copy dlls ***



py2exe会在当前目录下生成两个目录 build和dist
build里是一些py2exe运行时产生的中间文件,dist里有最终的可执行文件
 library.zip
 w9xpopen.exe
 python23.dll
 hello.exe

现在可以运行hello.exe了
E:\Projects\WorkSpace\Python\dist>;hello

Hello, World!


不过记得如果要发布到别的机器上时,library.zip、 w9xpopen.exe、python23.dll这几个文件是必须要和hello.exe在一起的。

好了,这次先到这里,下次我们做一个wxPython的例子

最后,大家试试运行
python setup.py py2exe --help

看看py2exe都有哪些参数



 xichen 回复于:2005-06-06 11:31:45

如果你的程序中有中文,在py2exe打包时一定要-p参数带上所有的包,否则中文显示出错。


 wolfg 回复于:2005-06-06 12:17:29

引用:原帖由 "xichen"]如果你的程序中有中文,在py2exe打包时一定要-p参数带上所有的包,否则中文显示出错。
 发表:



能具体解释一下吗?
我刚才试了一下
# hello.py

def main():
    print "Hello, World!"
print "你好!"

if __name__ == '__main__':
  main() 

好像也可以啊。
发现好像没有-p这个参数


 wolfg 回复于:2005-06-06 15:34:50

找到-p这个参数了,原来是这样的
python setup.py --help-commands
python setup.py --help py2exe
引用:Global options:
  --verbose (-v)  run verbosely (default)
  --quiet (-q)    run quietly (turns verbosity off)
  --dry-run (-n)  don't actually do anything
  --help (-h)     show detailed help message

Options for 'py2exe' command:
  --optimize (-O)    optimization level: -O1 for "python -O", -O2 for "python
                     -OO", and -O0 to disable [default: -O0]
  --dist-dir (-d)    directory to put final built distributions in (default is
                     dist)
  --excludes (-e)    comma-separated list of modules to exclude
  --ignores          comma-separated list of modules to ignore if they are not
                     found
  --includes (-i)    comma-separated list of modules to include
  --packages (-p)    comma-separated list of packages to include
  --compressed (-c)  create a compressed zipfile
  --xref (-x)        create and show a module crosss reference

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help




 cnsafety 回复于:2005-06-10 18:46:32

这个语言学着有用处吗??
汗..


 xichen 回复于:2005-06-13 11:49:20

如果你对本语言的用处有疑问,可以看本版置顶的一些帖子。


 wrxys 回复于:2005-06-16 05:05:58

这些.py的scripts应该放在哪个文件夹?


 xichen 回复于:2005-06-16 08:56:57

请详细提出你的问题,
如果你的疑问是将py放在哪个文件夹中才可以用py2exe打包的话。只要不包含中文目录名就可以了。




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=556861
转载请注明作者名及原文出处



收藏本页到: