py2exe生成单个可执行文件

2024-06-01 01:49:46 发布

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

我想我听说py2exe能够做到这一点,但我从未想到这一点。有人成功地做到了这一点吗?我可以查看setup.py文件吗?您使用了哪些命令行选项

基本上,我想它会给我一个单独的可执行文件,它可以将自身解压缩到maybe/temp并运行


Tags: 文件命令行py可执行文件选项setuptempmaybe
3条回答

使用py2exe执行此操作的方法是在setup.py文件中使用bundle_files选项。对于单个文件,您需要将bundle_files设置为1,compressed设置为True,并将zipfile选项设置为None。这样它就创建了一个压缩文件,便于分发

下面是直接从py2exe site*引用的bundle_file选项的更完整描述

Using "bundle_files" and "zipfile"

An easier (and better) way to create single-file executables is to set bundle_files to 1 or 2, and to set zipfile to None. This approach does not require extracting files to a temporary location, which provides much faster program startup.

Valid values for bundle_files are:

  • 3 (default) don't bundle
  • 2 bundle everything but the Python interpreter
  • 1 bundle everything, including the Python interpreter

If zipfile is set to None, the files will be bundle within the executable instead of library.zip.

下面是setup.py示例:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    windows = [{'script': "single.py"}],
    zipfile = None,
)

正如另一张海报所提到的,py2exe将生成一个可执行文件+一些要加载的库。您还可以将一些数据添加到程序中

下一步是使用安装程序,将所有这些打包成一个易于使用的可安装/不可安装程序

几年来,我一直很高兴地使用InnoSetup,并将其用于商业项目,因此我衷心推荐它

PyInstaller将创建一个没有依赖项的.exe文件;使用--onefile选项。它通过将所有需要的共享lib打包到可执行文件中,并在运行之前解压缩它们来实现这一点,正如您所描述的(EDIT:py2exe也有此功能,请参见minty's answer

我使用svn的PyInstaller版本,因为最新版本(1.3)有些过时。对于一个依赖于PyQt、PyQwt、numpy、scipy等的应用程序来说,它一直运行得非常好

相关问题 更多 >