2004/06/27

Python 脚本转换成WIN EXE的开源工具:Py2EXE

Py2EXE

昨天和今天,为了修改GML,从未接触过Python,硬着头皮干了一个通宵,终于有些感觉,GML再加上今天的完善,功能自觉已很强了。

Python的老家提供的win版,只有一个解释环境,用了一天,有些意思,比perl要爽。为了找个编译器,类似以前的perl2exe,搜了一下,找到这个开源工具,它的大体工作方式是把需要的库打包成ZIP文件,再加一个exe的loader,就成了独立于python解释环境的win exe程序了。


Assuming you have written a python script myscript.py which you want to convert into an executable windows program, able to run on systems without a python installation. If you don't already have written a distutils setup-script, write one, and insert the statement 'import py2exe' before the call to the setup function:

# setup.py
from distutils.core import setup
import py2exe

setup(console=["myscript.py"])

Running

python setup.py py2exe --help

will display all available command-line flags to the py2exe command.

Now you can call the setup script like in this way:

python setup.py py2exe

and a subdirectory dist will be created, containing the files myscript.exe, python23.dll, and library.zip. If your script uses compiled C extension modules, they will be copied here as well, also all dlls needed at runtime.

These files include everything that is needed for your program, and you should distribute the whole directory contents.

The above setup script creates a console program, if you want a GUI program without the console window, simply replace console=["myscript.py"] with windows=["myscript.py"].

py2exe can create more than one exe file in one run, this is useful if you have a couple of related scripts. Pass a list of all scripts in the console and/or windows keyword argument.




1 评论数:

At 3:35 上午, 笔戎 提交...

以为很容易使用,谁知让它编译时,就是不通过!不知何解

C:\temp\gml04>C:\Python23\python.exe setup.py py2exe
running py2exe
*** searching for required modules ***
*** parsing results ***
creating python loader for extension 'datetime'
Traceback (most recent call last):
File "setup.py", line 5, in ?
setup(windows=["gmlw.py"])
File "c:\Python23\lib\distutils\core.py", line 149, in setup
dist.run_commands()
File "c:\Python23\lib\distutils\dist.py", line 907, in run_commands
self.run_command(cmd)
File "c:\Python23\lib\distutils\dist.py", line 927, in run_command
cmd_obj.run()
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 190, in run
py_files, extensions = self.parse_mf_results(mf)
File "C:\Python23\Lib\site-packages\py2exe\build_exe.py", line 706, in parse_m
f_results
raise RuntimeError RuntimeError: Don't know how to handle ''C:\\Python23\\lib\\PmwColor.pyc''

 

Post a Comment

<< 首页