免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 17758 | 回复: 9
打印 上一主题 下一主题

py2exe初接触(一) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-06-05 23:34 |只看该作者 |倒序浏览
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的版本选择下载的文件
py2exe-0.5.4.win32-py2.3.exe

py2exe-0.5.4.win32-py2.4.exe

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

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

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

  4. if __name__ == '__main__':
  5.    main()
复制代码


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

  1. # setup.py
  2. from distutils.core import setup
  3. import py2exe
  4.       
  5. setup(console=["hello.py"])
复制代码


运行setup.py,记得要传一个参数给它
  1. 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了
  1. E:\Projects\WorkSpace\Python\dist>;hello
  2. Hello, World!
复制代码


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

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

最后,大家试试运行
  1. python setup.py py2exe --help
复制代码

看看py2exe都有哪些参数

论坛徽章:
0
2 [报告]
发表于 2005-06-06 11:31 |只看该作者

py2exe初接触(一)

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

论坛徽章:
0
3 [报告]
发表于 2005-06-06 12:17 |只看该作者

py2exe初接触(一)

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


能具体解释一下吗?
我刚才试了一下
  1. # hello.py
  2. def main():
  3.            print "Hello, World!"
  4.         print "你好!"

  5. if __name__ == '__main__':
  6.   main()
复制代码

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

论坛徽章:
0
4 [报告]
发表于 2005-06-06 15:34 |只看该作者

py2exe初接触(一)

找到-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

论坛徽章:
0
5 [报告]
发表于 2005-06-10 18:46 |只看该作者

py2exe初接触(一)

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

论坛徽章:
0
6 [报告]
发表于 2005-06-13 11:49 |只看该作者

py2exe初接触(一)

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

论坛徽章:
0
7 [报告]
发表于 2005-06-16 05:05 |只看该作者

py2exe初接触(一)

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

论坛徽章:
0
8 [报告]
发表于 2005-06-16 08:56 |只看该作者

py2exe初接触(一)

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

论坛徽章:
0
9 [报告]
发表于 2009-06-03 17:46 |只看该作者
我按照你说的做,为什么在屏幕上不能打印出hello,world呢

论坛徽章:
0
10 [报告]
发表于 2009-06-03 18:00 |只看该作者

回复 #9 gengzhifeng 的帖子

您真强,这么古老的帖子又翻出来了。下次请多google
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP