使用 py2exe 帮助将 .py 转换为 .exe [英] Help converting .py to .exe, using py2exe

查看:66
本文介绍了使用 py2exe 帮助将 .py 转换为 .exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的脚本

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

我试过了,但出现以下错误

I tried this but got the following error

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")

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

seem "unusual" and likely to be causing that "invalid command" message. I'd be curious to know the rationale for having them.

更新

一个常见的约定是将脚本命名为 setup.py(虽然在这种情况下它看起来名为 p2e_simple_con.py).其中的 setup 函数解析命令行,通常你希望用一个命令来运行它,例如:

A common convention is to name the script setup.py (although in this case it's named p2e_simple_con.py it seems). The setup function in it parses the command line, and usually you expect to run it with a command such as:

python setup.py py2exe

其中py2exe是py2exe包理解然后构建EXE文件的命令.所以只要安装了 py2exe 包,就应该接受该命令.

where py2exe is a command that the py2exe package understands and then builds the EXE file. So as long as the py2exe package is installed, that command should be accepted.

sys.argv.append("py2exe") 将该命令添加到命令行,因此在您的情况下,您应该能够输入:

The line sys.argv.append("py2exe") adds that command to the command-line, so in your case you should just be able to type:

python p2e_simple_con.py

构建 exe.但是,如果您还没有安装 py2exe 包,那么基本的 distutils 将无法识别 py2exe 命令.但在这种情况下,我希望您得到 import py2exe 行的异常.所以,我不确定发生了什么.

to build the exe. However, if you haven't installed the py2exe package, then the base distutils wouldn't recognise the py2exe command. But in that case, I'd expect you to get an exception for the line import py2exe. So, I'm not sure what's going on.

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

What is the command you used to run the setup program?

这篇关于使用 py2exe 帮助将 .py 转换为 .exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆