Cx_Freeze Compiled pygame File doesn't work

Cx_Freeze Compiled pygame File doesn't work

Postby Pedro H. Forli » Sat Aug 03, 2013 6:01 pm

The following error happens if i try to compile my python (using python 3.2) file:

Code: Select all
Traceback(most recent call last):
File
"c:\python32\lib\site-packages\cx_Freeze\initscripts\Console3.py", line
27, in <module>
  exec(code, m.__dict__)
 File "Abertura.py", line 208, in <module>
 File "Abertura.py", line 154, in main
 File "Abertura.py", line 9, in __init__
 pygame.error: Couldn't open
C:\Python32\build\exe.win32-3.2\library.zip\Imagens\menu1.png


I already included pygame._view and tried to copy the 'Imagens' directory to the library.zip file, but it doesn't work. I'm using images, musics and videos that come's from other directories by including in my code the following function:

Code: Select all
def file_path(filename, directory):
    return os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            directory,
            filename
        )

And this is my setup.py file:

Code: Select all
from cx_Freeze import setup, Executable

exe=Executable(
     script="Abertura.py",
     base="Win32Gui",
     )
includefiles=[('C:\Python32\Imagens', 'Imagens'), ('C:\Python32\Musicas','Musicas'),('C:\Python32\Videos','Videos')]
includes=[]
excludes=[]
packages=[]
setup(
     version = "1.0",
     description = "RPG",
     author = "Pedro Forli e Ivan Veronezzi",
     name = "Batalha Inapropriada",
     options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
     executables = [exe]
     )

How do i fix it?
(sorry about my possibles english mistakes)
Pedro H. Forli
 
Posts: 14
Joined: Thu Aug 01, 2013 1:50 am

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Mekire » Sun Aug 04, 2013 2:48 pm

Well, I'm not sure. The first thing I notice is you are using unescaped backslashes in path names which are bound to give you trouble at some point. Aside from that, I wonder why your program is setup so strangely? Why aren't your resources in a place that your script can find them relative to itself, rather than having to jump through hoops? The resources at least in my experience don't end up packed inside the library.zip either. Is your setup failing to make an exe in the first place or is it creating one and then failing when you try to run it?

-Mek
User avatar
Mekire
 
Posts: 1125
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Pedro H. Forli » Mon Aug 05, 2013 2:55 am

Mekire wrote:Well, I'm not sure. The first thing I notice is you are using unescaped backslashes in path names which are bound to give you trouble at some point. Aside from that, I wonder why your program is setup so strangely? Why aren't your resources in a place that your script can find them relative to itself, rather than having to jump through hoops? The resources at least in my experience don't end up packed inside the library.zip either. Is your setup failing to make an exe in the first place or is it creating one and then failing when you try to run it?

-Mek

Creating one, then failing to run it
Pedro H. Forli
 
Posts: 14
Joined: Thu Aug 01, 2013 1:50 am

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Mekire » Mon Aug 05, 2013 3:03 am

Well, I would suggest you restructure your program so the resources can be loaded relative to the main module. Is there something preventing you from doing this? The program shouldn't be trying to find your files within the library.zip in the first place, which makes me think your __file__ loading function is not giving the correct directory when you run the exe.

-Mek
User avatar
Mekire
 
Posts: 1125
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Pedro H. Forli » Mon Aug 05, 2013 9:04 pm

Mekire wrote:Well, I would suggest you restructure your program so the resources can be loaded relative to the main module. Is there something preventing you from doing this? The program shouldn't be trying to find your files within the library.zip in the first place, which makes me think your __file__ loading function is not giving the correct directory when you run the exe.

-Mek


I've created modules (such as player, enemy, menu). Each have their image, spritesheet,videos and effect sounds files, so i use different resources in different modules, loading them apart from the main one. I don't exactly know how i could load everything in the main module, i only have been programming in python for the past month, so i didn't exactly have learned yet how to manipulate directories. I think i'll just put this on hold and study a bit more and try to do what you are saying. If doesn't work out i'll ask again
Pedro H. Forli
 
Posts: 14
Joined: Thu Aug 01, 2013 1:50 am

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Mekire » Mon Aug 05, 2013 11:25 pm

The loading doesn't necessarily need to occur in the main file, but when you run a program the current working directory is set to the main file automatically, so other modules that are imported can use paths relative to that. Usually I load all resources in one module and place them in a dictionary.


This program is organized as a package, but take a look:

This file is imported and it initializes the display and calls resource loading functions.
https://github.com/metulburr/Plants-VS-Zombies/blob/master/data/setup.py

The actual resource loading functions are in here:
https://github.com/metulburr/Plants-VS-Zombies/blob/master/data/tools.py#L146

Then every module that wants to use these resources imports the setup.py script.

-Mek
User avatar
Mekire
 
Posts: 1125
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Pedro H. Forli » Tue Aug 06, 2013 12:45 am

Thank you Mek. I haven't done what you told me too, but by cutting off the line:

Code: Select all
os.path.dirname(os.path.abspath(__file__))


From the file_path function, for some reason, worked out just fine
Pedro H. Forli
 
Posts: 14
Joined: Thu Aug 01, 2013 1:50 am

Re: Cx_Freeze Compiled pygame File doesn't work

Postby Mekire » Tue Aug 06, 2013 1:39 am

Hmm, yeah it sounds like your files were already in a place they could be found relative to your modules. I thought it was the __file__ thing that was throwing it off. The cx_freeze exe made __file__ point to the library.zip on execution for whatever reason.

Anyway, congrats on getting it to work.
-Mek
User avatar
Mekire
 
Posts: 1125
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest