Python 使用py2exe帮助将.py转换为.exe

Python 使用py2exe帮助将.py转换为.exe,python,py2exe,Python,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 na

我运行的脚本

# 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'
台词:

sys.argv.append("py2exe")
sys.argv.append("-q")
看起来“不寻常”,可能导致“无效命令”消息。我很想知道拥有它们的理由

更新

一个常见的约定是将脚本命名为
setup.py
(尽管在本例中,它似乎命名为
p2e\u simple\u 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
的异常。所以,我不知道发生了什么

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

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

由于从错误消息中似乎有一个参数被赋予了它不接受的
py2exe
,我猜它是
-q
参数。因此,请尝试删除行
sys.argv.append(“-q”)
,然后运行脚本


我自己没有使用过py2exe,所以我无法真正测试它。这只是一个想法。

这两个警告是由于选项字典中的逗号错误,位于最终值之后。你想要什么

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

试试看这是否能修复您的错误。

问题可能是
py2exe
是用于python3的

Python2的版本似乎是
py2exe2msi


我在Python2中也遇到了类似的错误,尝试编写为Python3版本编写的代码。

您是否已经阅读了本教程。。。