152

我以为我听说py2exe能够做到这一点,但我从来没有想通。有没有人成功做到这一点?我可以查看您的 setup.py 文件,以及您使用的命令行选项吗?

基本上我正在考虑它给我一个可执行文件,它可以执行类似将自身解压缩到 /temp 并运行的操作。

4

9 回答 9

178

使用 py2exe 的方法是在 setup.py 文件中使用 bundle_files 选项。对于单个文件,您需要设置bundle_files为 1、compressedTrue,并将 zipfile 选项设置为 None。这样,它会创建一个压缩文件以便于分发。

这是直接从py2exe 站点引用的 bundle_file 选项的更完整描述*

使用“bundle_files”和“zipfile”

创建单文件可执行文件的一种更简单(更好)的方法是将 bundle_files 设置为 1 或 2,并将 zipfile 设置为 None。这种方法不需要将文件提取到临时位置,从而提供更快的程序启动。

bundle_files 的有效值为:

  • 3(默认)不捆绑
  • 2 捆绑除 Python 解释器之外的所有内容
  • 1 捆绑一切,包括 Python 解释器

如果 zipfile 设置为 None,则文件将捆绑在可执行文件中,而不是 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,
)
于 2008-09-22T03:19:08.937 回答
109

PyInstaller将创建一个没有依赖关系的单个 .exe 文件;使用该--onefile选项。它通过将所有需要的共享库打包到可执行文件中来做到这一点,并在运行之前将它们解包,就像您描述的那样(编辑:py2exe 也具有此功能,请参阅minty 的答案

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

于 2008-09-22T00:55:46.240 回答
15

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

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

几年来我一直很高兴地使用InnoSetup并用于商业程序,所以我衷心推荐它。

于 2008-12-02T09:44:45.113 回答
8

我已经能够创建一个将所有资源嵌入到 exe 中的单个 exe 文件。我正在窗户上建造。所以这将解释我正在使用的一些 os.system 调用。

首先,我尝试将所有图像转换为位图,然后将所有数据文件转换为文本字符串。但这导致最终的exe非常大。

在谷歌搜索一周后,我想出了如何更改 py2exe 脚本以满足我的需求。

这是我提交的关于 sourceforge 的补丁链接,请发表评论,以便我们将其包含在下一个发行版中。

http://sourceforge.net/tracker/index.php?func=detail&aid=3334760&group_id=15583&atid=315583

这解释了所做的所有更改,我只是在设置行中添加了一个新选项。这是我的 setup.py。

我会尽力评论它。请知道我的 setup.py 很复杂,因为我通过文件名访问图像。所以我必须存储一个列表来跟踪它们。

这是我试图制作的一个想要的屏幕保护程序。

我使用 exec 在运行时生成我的设置,这样更容易剪切和粘贴。

exec "setup(console=[{'script': 'launcher.py', 'icon_resources': [(0, 'ICON.ico')],\
      'file_resources': [%s], 'other_resources': [(u'INDEX', 1, resource_string[:-1])]}],\
      options={'py2exe': py2exe_options},\
      zipfile = None )" % (bitmap_string[:-1])

分解

script = py script 我想转成 exe

icon_resources = exe的图标

file_resources = 我想嵌入到 exe 中的文件

other_resources = 嵌入到 exe 中的字符串,在本例中为文件列表。

options = py2exe 用于将所有内容创建到一个 exe 文件中的选项

bitmap_strings = 要包含的文件列表

请注意,在您按照上面链接中的说明编辑 py2exe.py 文件之前,file_resources 不是有效选项。

我第一次尝试在此站点上发布代码,如果我弄错了,请不要激怒我。

from distutils.core import setup
import py2exe #@UnusedImport
import os

#delete the old build drive
os.system("rmdir /s /q dist")

#setup my option for single file output
py2exe_options = dict( ascii=True,  # Exclude encodings
                       excludes=['_ssl',  # Exclude _ssl
                                 'pyreadline', 'difflib', 'doctest', 'locale',
                                 'optparse', 'pickle', 'calendar', 'pbd', 'unittest', 'inspect'],  # Exclude standard library
                       dll_excludes=['msvcr71.dll', 'w9xpopen.exe',
                                     'API-MS-Win-Core-LocalRegistry-L1-1-0.dll',
                                     'API-MS-Win-Core-ProcessThreads-L1-1-0.dll',
                                     'API-MS-Win-Security-Base-L1-1-0.dll',
                                     'KERNELBASE.dll',
                                     'POWRPROF.dll',
                                     ],
                       #compressed=None,  # Compress library.zip
                       bundle_files = 1,
                       optimize = 2                        
                       )

#storage for the images
bitmap_string = '' 
resource_string = ''
index = 0

print "compile image list"                          

for image_name in os.listdir('images/'):
    if image_name.endswith('.jpg'):
        bitmap_string += "( " + str(index+1) + "," + "'" + 'images/' + image_name + "'),"
        resource_string += image_name + " "
        index += 1

print "Starting build\n"

exec "setup(console=[{'script': 'launcher.py', 'icon_resources': [(0, 'ICON.ico')],\
      'file_resources': [%s], 'other_resources': [(u'INDEX', 1, resource_string[:-1])]}],\
      options={'py2exe': py2exe_options},\
      zipfile = None )" % (bitmap_string[:-1])

print "Removing Trash"
os.system("rmdir /s /q build")
os.system("del /q *.pyc")
print "Build Complete"

好的,这就是 setup.py 现在访问图像所需的魔法。我在没有考虑 py2exe 的情况下开发了这个应用程序,然后添加了它。所以你会看到这两种情况的访问权限。如果找不到图像文件夹,它会尝试从 exe 资源中提取图像。代码将解释它。这是我的精灵类的一部分,它使用directx。但你可以使用任何你想要的 api 或者只访问原始数据。没关系。

def init(self):
    frame = self.env.frame
    use_resource_builtin = True
    if os.path.isdir(SPRITES_FOLDER):
        use_resource_builtin = False
    else:
        image_list = LoadResource(0, u'INDEX', 1).split(' ')

    for (model, file) in SPRITES.items():
        texture = POINTER(IDirect3DTexture9)()
        if use_resource_builtin: 
            data = LoadResource(0, win32con.RT_RCDATA, image_list.index(file)+1) #windll.kernel32.FindResourceW(hmod,typersc,idrsc)               
            d3dxdll.D3DXCreateTextureFromFileInMemory(frame.device,   #Pointer to an IDirect3DDevice9 interface
                                              data,                #Pointer to the file in memory
                                              len(data),           #Size of the file in memory
                                              byref(texture))      #ppTexture
        else:
            d3dxdll.D3DXCreateTextureFromFileA(frame.device, #@UndefinedVariable
                                               SPRITES_FOLDER + file,
                                               byref(texture))            
        self.model_sprites[model] = texture
    #else:
    #    raise Exception("'sprites' folder is not present!")

任何问题都可以自由提问。

于 2011-07-26T03:20:59.760 回答
4

如前所述,您应该创建一个安装程序。即使通过将 bundle_files 选项设置为 1 并将 zipfile 关键字参数设置为 None 也可以让 py2exe 将所有内容捆绑到一个可执行文件中,但我不建议将其用于 PyGTK 应用程序。

这是因为 GTK+ 试图从加载它的目录加载它的数据文件(本地、主题等)。因此,您必须确保可执行文件的目录还包含 GTK+ 使用的库以及安装 GTK+ 的目录 lib、share 等。否则,在系统范围内未安装 GTK+ 的机器上运行应用程序时会遇到问题。

有关更多详细信息,请阅读我的 PyGTK 应用程序 py2exe 指南。它还解释了如何捆绑除 GTK+ 之外的所有内容。

于 2010-09-13T17:40:51.543 回答
1

我被告知bbfreeze将创建一个文件 .EXE,并且比 py2exe 更新。

于 2009-02-19T04:02:07.390 回答
-2

我最近使用 py2exe 创建了一个用于后审的可执行文件,用于将评论发送到 ReviewBoard。

这是我使用的 setup.py

from distutils.core import setup
import py2exe

setup(console=['post-review'])

它创建了一个包含 exe 文件和所需库的目录。我认为不可能使用 py2exe 来获取单个 .exe 文件。如果您需要,您将需要首先使用 py2exe,然后使用某种形式的安装程序来制作最终的可执行文件。

需要注意的一件事是,您在应用程序中使用的任何 egg 文件都需要解压缩,否则 py2exe 无法包含它们。这在 py2exe 文档中有介绍。

于 2008-09-22T00:56:28.343 回答
-2

尝试 c_x freeze 它可以创建一个很好的独立

于 2015-08-04T08:13:18.140 回答
-6

不,它不会为您提供一个可执行文件,因为您之后只有一个文件 - 但您有一个目录,其中包含运行程序所需的所有内容,包括一个 exe 文件。

我今天刚写了这个 setup.py。您只需要调用python setup.py py2exe.

于 2008-09-22T00:53:21.957 回答