2010-08-01 88 views
0

我跑帮助转换的.py到.EXE,使用py2exe

# p2e_simple_con.py 
# very simple script to make an executable file with py2exe 
# put this script and your code script into the same folder 
# run p2e_simple_con.py 
# it will create a subfolder 'dist' where your exe file is in 
# has the same name as the script_file with extension exe 
# (the other subfolder 'build' can be deleted) 
# note: with console code put a wait line at the end 

from distutils.core import setup 
import py2exe 
import sys 

sys.argv.append("py2exe") 
sys.argv.append("-q") 

# this is your code script file, change it as needed 
# use it in the working directory or give full path 
script_file = 'DateTime_Test1.py' 

# create a single .exe file and use compression 
# (the .exe file is 30% larger with no compression) 
setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': 1,}}, 
    zipfile = None, 
    # replace console with windows for a GUI program 
    console = [{'script': script_file}] 
) 

脚本我试过,但有以下错误

C:\Python26\Lib\distutils\dist.py:266: UserWarning: Unknown distribution option: 'console' 
    warnings.warn(msg) 
C:\Python26\Lib\distutils\dist.py:266: UserWarning: Unknown distribution option: 'zipfile' 
    warnings.warn(msg) 
usage: py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 
    or: py2exe.py --help [cmd1 cmd2 ...] 
    or: py2exe.py --help-commands 
    or: py2exe.py cmd --help 

error: invalid command 'py2exe' 
+0

你有没有经历过这个教程... http://www.py2exe.org/index.cgi/Tutorial?highlight =((Tutorial)) – 2010-08-01 03:30:39

回答

1

的线条:

sys.argv.append("py2exe") 
sys.argv.append("-q") 

似乎“不寻常”,并可能导致“无效命令”消息。我很想知道拥有它们的理由。

更新

一个通常的规则是命名脚本setup.py(虽然在这种情况下,它的命名p2e_simple_con.py似乎)。在它的setup函数解析的命令行,通常你希望用一个命令,如运行它:

python setup.py py2exe 

其中py2exe是,py2exe包能够理解的命令,然后生成EXE文件。所以只要安装了py2exe软件包,就应该接受该命令。

线sys.argv.append("py2exe")补充说,命令的命令行,所以你的情况,你就应该能够键入:

python p2e_simple_con.py 

打造的exe。但是,如果您尚未安装py2exe软件包,则基址distutils将无法​​识别py2exe命令。但是在那种情况下,我希望你得到import py2exe这一行的例外。所以,我不确定发生了什么事。

用来运行安装程序的命令是什么?

+0

我在另一个论坛上看到了代码,并且不确定自己。 – rectangletangle 2010-08-01 04:23:03

0

这只是一个想法(不是真的答案,但我不能评论)。

由于从错误消息看来有一个给出的参数py2exe它不接受,所以我猜想它是-q参数。因此,请尝试删除sys.argv.append("-q")行,然后运行脚本。

我没有使用py2exe我自己所以我不能真正测试这个。这只是一个想法。

-1

这两个警告是由于选项字典中的错误逗号,在最终值之后。你想拥有

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': 1}}, 
    zipfile = None, 
    # replace console with windows for a GUI program 
    console = [{'script': script_file}] 
) 

试试看看能否解决你的错误。

0

的问题可能是py2exe是蟒蛇3,

一个为蟒2似乎是py2exe2msi

我也越来越类似的错误在Python 2,试图做的代码,这是为python 3版本编写。