Creating a Python .exe file: py2exe invalid image error

喜你入骨 提交于 2019-12-07 19:41:32

问题


I want to create a .exe file. I'm using Python 2.7.3 with wxPython for the GUI. I've installed py2exe for Python 2.7 and tried to create a .exe file following the tutorial at http://www.py2exe.org/index.cgi/Tutorial

When I try to run my created .exe file, I get following error:

File "wx\_gdi.pyc",line823, in BitmapFromImage wx._core.PyAssertionError: 
C++ assertion "image.OK()" failed at ..\..\src\msw\bitmap.cpp(802) in 
wxBitmap::CreateFromImage(): invalid image

So I looked into my code and the following line is causing the problem:

self.bmpSun = wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(wx.Image('images/sun.gif', wx.BITMAP_TYPE_ANY)), pos = (0,0))

When I browse to the source folder and run the main.py file myself, my app runs fine. I haven't found any help online so far. Can anybody solve this problem/suggest reliable alternatives for py2exe? Thank you.


回答1:


The line that errors out is looking for an image in the Images folder. That's a path relative to the .exe file created by py2exe. So you need to be sure that that folder exists in correct position relative to the exe, and that it is populated with the images you are going to use. You can do this 2 ways. Either copy the folder to where the exe will reside, or use the data_files keyword arg in the script that makes the .exe. Here's the pertinent part of one of my setup scripts, showing a data_files list of tuples and use of the data_files keyword arg later:

data_files = [('Images', glob('Images/*.*')),
                            ]

includes = ['win32com.decimal_23', 'datetime']

excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses',  'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter', 'unittest']
packages = []

dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
                'tk84.dll','MSVCP90.dll']

setup(
    data_files = data_files,
    options = {"py2exe": {"compressed": 2,
                          "optimize": 2,
                          "includes": includes,
                          "excludes": excludes,
                          "packages": packages,
                          "dll_excludes": dll_excludes,
                          "bundle_files": 1,
                          "dist_dir": "dist",
                          "xref": False,
                          "skip_archive": False,
                          "ascii": False,
                          "custom_boot_script": '',
                         }
              },
    zipfile = None,
    windows = [filename]
    )


来源:https://stackoverflow.com/questions/12829261/creating-a-python-exe-file-py2exe-invalid-image-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!