首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >py2exe -生成单个可执行文件

py2exe -生成单个可执行文件
EN

Stack Overflow用户
提问于 2008-09-22 00:49:12
回答 8查看 183.3K关注 0票数 152

我以为我听说py2exe能够做到这一点,但我从来没有弄明白。有人成功地做到了这一点吗?我可以看一下您的setup.py文件吗?您使用了哪些命令行选项?

基本上,我认为它给了我一个可执行文件,它可以自己解压成/temp,然后运行。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2008-09-22 00:55:46

PyInstaller将创建一个没有依赖项的.exe文件;使用--onefile选项。它通过将所有需要的共享库打包到可执行文件中,然后在运行之前将它们解压缩,就像你所描述的那样(编辑: py2exe也有这个功能,请参阅minty's answer)

我使用来自svn的PyInstaller版本,因为最新的版本(1.3)有点过时。对于一个依赖于PyQt,PyQwt,numpy,scipy和更多的应用程序来说,它工作得非常好。

票数 109
EN

Stack Overflow用户

发布于 2008-12-02 09:44:45

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

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

几年来,我一直很高兴地使用InnoSetup,并将其用于商业程序,因此我强烈推荐它。

票数 15
EN

Stack Overflow用户

发布于 2011-07-26 11:21:00

我已经能够创建一个单独的exe文件,所有资源都嵌入到exe中。我是在windows上构建的。这就解释了我正在使用的一些os.system调用。

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

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

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

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

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

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

这是我正在尝试制作的一个want- to -b屏幕保护程序。

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

代码语言:javascript
复制
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脚本我要转换为可执行文件

icon_resources =可执行文件的图标

file_resources =要嵌入到可执行文件中的文件

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

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

bitmap_strings =要包含的文件列表

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

这是我第一次尝试在这个网站上发布代码,如果我写错了,请不要责备我。

代码语言:javascript
复制
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资源中拉出图像。代码将对此进行解释。这是我的sprite类的一部分,它使用directx。但是您可以使用任何您想要的api,或者只访问原始数据。这不重要。

代码语言:javascript
复制
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!")

任何问题都可以随意问。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/112698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档