免费注册 查看新帖 |

Chinaunix

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

py2exe 接触和使用(1) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-25 16:11 |只看该作者 |倒序浏览
py2exe 的简介

    py2exe是一种python发布工具,可以用来将 python 脚本转换成 windows 下的可执行程序,而且不需要安装 python 环境便可运行。

    py2exe 现在可以用来创建使用了 wxPython, Tkinter, Pmw, PyGTK, pygame, win32com client 以及 server 等模块的程序。

    py2exe 被用于很多的著名的软件,如  BitTorrent, SpamBayes, 等等。

    更详细的介绍以及使用可以看它的官方网站
http://www.py2exe.org/

    下面让我们一起接触 py2exe 并来简单的使用一下吧!
1. 下载安装py2exe

    py2exe目前的版本是 0.6.6,根据你安装的 python 的版本选择下载的文件,具体的选择,请参见 SourceForge 上发布的信息。

   
http://sourceforge.net/project/showfiles.php?group_id=15583

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

2.  使用 py2exe

    让我们先准备一个简单的 python 程序 hello.py
# hello.py
    def main():
        print "hello, python"
        
    if __name__ == '__main__':
        main()

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

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

    这里 console=[...] 配置参数指示了几个信息:
        * 我们需要生成的是一个 console 应用程序,当然,如果你的应用是 gui 的,那就应该是 windows=[...] 了
        * [...] 中需要列出你为了生成该 console 应用程序所使用的脚本列表,如我们只有一个 hello.py
        * 另外,当前的 py2exe 还支持如下的一些生成:
            - com_servers = [...] # list of fully qualified class names to build into the exe com server
            - service = [...] # list of fully qualified class names to build into a service executable
            - zipfile = "xxx.zip" # filename of the zipfile containing the pure Python modules

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

        python setup.py py2exe

    之后你应该会看到一些如下的输出信息:

      --------
      
      E:\workspaces\Pythons\hello>python setup.py py2exe
      running py2exe
      *** searching for required modules ***
      *** parsing results ***
      creating python loader for extension 'unicodedata'
      creating python loader for extension 'bz2'
      *** finding dlls needed ***
      *** create binaries ***
      *** byte compile python files ***
      skipping byte-compilation of D:\Python25\lib\StringIO.py to StringIO.pyc
      skipping byte-compilation of D:\Python25\lib\UserDict.py to UserDict.pyc
      skipping byte-compilation of D:\Python25\lib\atexit.py to atexit.pyc
      skipping byte-compilation of D:\Python25\lib\base64.py to base64.pyc
      skipping byte-compilation of D:\Python25\lib\codecs.py to codecs.pyc
      skipping byte-compilation of D:\Python25\lib\copy.py to copy.pyc
      skipping byte-compilation of D:\Python25\lib\copy_reg.py to copy_reg.pyc
      skipping byte-compilation of D:\Python25\lib\encodings\__init__.py to encodings\__init__.pyc
      skipping byte-compilation of D:\Python25\lib\encodings\aliases.py to encodings\aliases.pyc
      
      ... ...
      
      byte-compiling E:\workspaces\Pythons\hello\build\bdist.win32\winexe\temp\bz2.py to bz2.pyc
      byte-compiling E:\workspaces\Pythons\hello\build\bdist.win32\winexe\temp\unicode
      data.py to unicodedata.pyc
      *** copy extensions ***
      *** copy dlls ***
      copying D:\Python25\lib\site-packages\py2exe\run.exe -> E:\workspaces\Pythons\he
      llo\dist\hello.exe
      
      *** binary dependencies ***
      Your executable(s) also depend on these dlls which are not included,
      you may or may not need to distribute them.
      
      Make sure you have the license if you distribute any of them, and
      make sure you don't distribute files belonging to the operating system.
      
         ADVAPI32.dll - C:\WINDOWS\system32\ADVAPI32.dll
         USER32.dll - C:\WINDOWS\system32\USER32.dll
         SHELL32.dll - C:\WINDOWS\system32\SHELL32.dll
         KERNEL32.dll - C:\WINDOWS\system32\KERNEL32.dll
      
      -------------

    py2exe 会在当前目录下生成两个目录 build 和 dist
    其中 build 里是一些 py2exe 运行时产生的中间文件,dist 里有最终的可执行文件

      E:\workspaces\Pythons\hello\dist 的目录
      2007/08/25  15:48              .
      2007/08/25  15:48              ..
      2006/09/19  09:52            77,824 bz2.pyd
      2007/08/25  15:48            16,384 hello.exe
      2007/08/25  15:48           707,607 library.zip
      2003/02/21  20:42           348,160 MSVCR71.dll
      2007/03/09  18:08         2,109,440 python25.dll
      2006/09/19  09:52           475,136 unicodedata.pyd

    下面运行一下 hello.exe 吧

      E:\workspaces\Pythons\hello\dist>hello.exe
      hello, python

    注:如果你需要将应用发布到其他的主机上时,bz2.pyd, library.zip, MSVCR71.dll, unicodedata.pyd, python25.dll 这几个文件是必须要和 hello.exe 在一起的。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29227/showart_367576.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP