Where to put images folder in python exe?

时光总嘲笑我的痴心妄想 提交于 2021-02-16 18:35:47

问题


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


回答1:


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.




回答2:


It looks like you've already fixed the image problem by moving the folder into dist. The missing font module, on the other hand, is a known problem between pygame and py2exe. Py2exe doesn't copy some necessary DLLs, so you have to override py2exe's isSystemDLL method, forcing it to include audio and font related DLLs.

If Table_Wars.py is the only module in your project, try running this script with python setup.py py2exe:

from os.path import basename
from distutils.core import setup

import py2exe

origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
    if basename(pathname).lower() in ("libogg-0.dll", "sdl_ttf.dll"):
        return 0
    return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL

setup(windows=[{"script": "Table_Wars.py"}],
      options={"py2exe": {"dist_dir": "dist"}})

You could also try the example py2exe setup file on the pygame wiki. If neither of them are working, please add the error messages to your question.


I tried running py2exe on a sample project, and it also breaks for me when I use the default pygame font. If you're using the default font, try putting a ttf file in the root of your project and also in the dist folder. You'll have to change the call to pygame.Font in your script as well:

font = pygame.font.Font("SomeFont.ttf", 28)


来源:https://stackoverflow.com/questions/10886732/where-to-put-images-folder-in-python-exe

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