帮助使用py2ex将.py转换为.exe

2024-06-06 15:10:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我写的剧本

# 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}]
)

我试过了,但得到了以下错误

^{pr2}$

Tags: thepyimportyouriswithsysscript
3条回答

问题可能是py2exe是针对python3的

python2的一个似乎是py2exe2msi

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

台词:

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

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

更新

一个常见的约定是将脚本命名为setup.py(尽管在本例中,它似乎被命名为p2e_simple_con.py)。其中的setup函数解析命令行,通常您希望使用以下命令运行它:

^{pr2}$

其中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,所以我不能真正测试这个。只是个主意。在

相关问题 更多 >