Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put images folder in python exe?

I have converted a python game I designed into an exe. Running the exe itself causes it to flash and then close, meaning an error has occured. Running it from the Command Prompt causes the error as well, but documents it:

Cannot load image: Playfield.png
Couldn't open images\Playfield.png

This is telling me that the load_image block is failing. I have encountered this before when I did not have an images directory.

I attempted to move the images folder to the dist directory. This is the error that shows up:

Traceback (most recent call last):
    File "Table_Wars.py", line 728, in <module>
    File "Table_Wars.py", line 51, in main
    File "Table_Wars.py", line 236, in __init__
    File "pygame\__init__.pyc", line 70, in __getattr__
NotImplementedError: font module not available
(ImportError: DLL load failed: The specified module could not be found.)

This is my first time with py2exe, so I'm not really sure what is happening. The raw python file itself, Table_Wars.py, runs as expected.

If it helps, the location for the entire Table_Wars folder is inside a folder called Games, located on my Desktop (C:\Users\Oventoaster\Desktop\Games\Table_Wars). I am running Windows 7 32 bit OS.

On request, here is the output.txt I have generated:

Folder PATH listing for volume OS
Volume serial number is 7659-4C9C
C:\USERS\OVENTOASTER\DESKTOP\GAMES\TABLE_WARS
build
   bdist.win32
       winexe
           bundle-2.7
           collect-2.7
              ctypes
              distutils
              email
                 mime
              encodings
              logging
              multiprocessing
                 dummy
              pygame
                 threads
              unittest
              xml
                  parsers
           temp
dist
images

Here is the setup.py I used to convert the file:

from distutils.core import setup
import py2exe

setup(console=['Table_Wars.py'])

EDIT: I have attempted to use the full py2exe example. This will create the exe, but gives the same Cannot load image error. Attempting to put the images folder in the same folder as the exe creates a Runtime Error: The application requested the runtime to terminate it in an unusual way.

The shortened form of the code Slace Diamond suggested prevents py2exe from finding Table_Wars.py:

from cmd:

running py2exe
*** searching for required modules ***
error: Table_Wars.py: No such file or directory.

setup and Table_Wars are in the same directory. If it help, I input the full path to python.exe and setup.py.

EDIT: I seem to be getting closer. I put the images directory within self.extra_datas, and now I am getting this:

Fatal Python error: (segmentation fault) This application has requested the runtime to terminate it in an unusual way. Please contact the application's suppourt team for more information

like image 663
Oventoaster Avatar asked Jun 11 '26 23:06

Oventoaster


1 Answers

When you build a distributable package with py2exe (and py2app for that matter), part of the package environment is to point to a local resource location for files. In your plain unpackaged version, you are referring to a relative "images/" location. For the packaged version, you need to configure your setup.py to include the resources in its own location.

Refer to this doc for very specific info about how to set the data_files option of your package: http://www.py2exe.org/index.cgi/data_files

That page has multiple examples to show both very simple paths, and also a helper function for finding the data and building the data_files list for you.

Here is an example of the simple snippet:

from distutils.core import setup
import py2exe

Mydata_files = [('images', ['c:/path/to/image/image.png'])]

setup(
    console=['trypyglet.py.py']
    data_files = Mydata_files
    options={
                "py2exe":{
                        "unbuffered": True,
                        "optimize": 2,
                        "excludes": ["email"]
                }
        }
)

This closely matches what you are trying to achieve. It is saying that the "image.png" source file should be placed into the "images" directory at the root of the resources location inside the package. This resource root will be your current directory from your python scripts, so you can continue to refer to it as a relative sub directory.

like image 198
jdi Avatar answered Jun 14 '26 06:06

jdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!