繁体   English   中英

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

[英]Help converting .py to .exe, using 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'

这些行:

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行中得到一个例外。 因此,我不确定发生了什么。

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

问题可能是py2exe适用于python 3,

python 2的似乎是py2exe2msi

我在python 2中也遇到类似的错误,试图执行为python 3版本编写的代码。

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

由于从错误消息看来,有一个不接受的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}]
)

尝试看看是否可以解决您的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM