[Pygame] compiling an exe

This is the place for queries that don't fit in any of the other categories.

[Pygame] compiling an exe

Postby metulburr » Mon May 13, 2013 12:42 am

This is the sript from pygame.org to compile the script.

I have gotten this to work to a certain extent, it creates a dir "dist" with a so called executable game.exe inside. However running the exe, creates a log file with the traceback.

I have no idea what this traceback means or refers to?
C:\Python27\dist\game.exe\zipextimporter.py:82: RuntimeWarning: import display: No module named _view
(ImportError: No module named _view)
C:\Python27\dist\game.exe\zipextimporter.py:82: RuntimeWarning: import draw: No module named _view
(ImportError: No module named _view)
C:\Python27\dist\game.exe\zipextimporter.py:82: RuntimeWarning: import image: No module named _view
(ImportError: No module named _view)
C:\Python27\dist\game.exe\zipextimporter.py:82: RuntimeWarning: import pixelcopy: No module named _view
(ImportError: No module named _view)
C:\Python27\dist\game.exe\zipextimporter.py:82: RuntimeWarning: import transform: No module named _view
(ImportError: No module named _view)
Traceback (most recent call last):
File "game.py", line 258, in <module>
File "game.py", line 154, in __init__
File "pygame\__init__.pyo", line 70, in __getattr__
NotImplementedError: display module not available
(ImportError: No module named _view)


Although i am running windows in a virtualbox, maybe that would be a reasoning to a possible display not available? I dont really know, i havent used windows in such a long time. I tried py2exe and cx_freeze and both result in this same traceback


the setup file:
Code: Select all
try:
    from distutils.core import setup
    import py2exe, pygame
    from modulefinder import Module
    import glob, fnmatch
    import sys, os, shutil
    import operator
except ImportError as message:
    raise SystemExit("Unable to load module. %s" % message)
 
#hack which fixes the pygame mixer and pygame font
origIsSystemDLL = py2exe.build_exe.isSystemDLL # save the orginal before we edit it
def isSystemDLL(pathname):
    # checks if the freetype and ogg dll files are being included
    if os.path.basename(pathname).lower() in ("libfreetype-6.dll", "libogg-0.dll","sdl_ttf.dll"): # "sdl_ttf.dll" added by arit.
            return 0
    return origIsSystemDLL(pathname) # return the orginal function
py2exe.build_exe.isSystemDLL = isSystemDLL # override the default function with this one
 
class pygame2exe(py2exe.build_exe.py2exe): #This hack make sure that pygame default font is copied: no need to modify code for specifying default font
    def copy_extensions(self, extensions):
        #Get pygame default font
        pygamedir = os.path.split(pygame.base.__file__)[0]
        pygame_default_font = os.path.join(pygamedir, pygame.font.get_default_font())
 
        #Add font to list of extension to be copied
        extensions.append(Module("pygame.font", pygame_default_font))
        py2exe.build_exe.py2exe.copy_extensions(self, extensions)
 
class BuildExe:
    def __init__(self):
        #Name of starting .py
        self.script = "C:\\Python27\\game.py"
 
        #Name of program
        self.project_name = "game"
 
        #Project url
        self.project_url = "about:none"
 
        #Version of program
        self.project_version = "0.0"
 
        #License of the program
        self.license = "MyApps License"
 
        #Auhor of program
        self.author_name = "Me"
        self.author_email = "example@example.com"
        self.copyright = "Copyright (c) 2009 Me."
 
        #Description
        self.project_description = "MyApps Description"
 
        #Icon file (None will use pygame default icon)
        self.icon_file = None
 
        #Extra files/dirs copied to game
        self.extra_datas = []
 
        #Extra/excludes python modules
        self.extra_modules = []
        self.exclude_modules = []
       
        #DLL Excludes
        self.exclude_dll = ['']
        #python scripts (strings) to be included, seperated by a comma
        self.extra_scripts = []
 
        #Zip file name (None will bundle files in exe instead of zip file)
        self.zipfile_name = None
 
        #Dist directory
        self.dist_dir ='dist'
 
    ## Code from DistUtils tutorial at http://wiki.python.org/moin/Distutils/Tutorial
    ## Originally borrowed from wxPython's setup and config files
    def opj(self, *args):
        path = os.path.join(*args)
        return os.path.normpath(path)
 
    def find_data_files(self, srcdir, *wildcards, **kw):
        # get a list of all files under the srcdir matching wildcards,
        # returned in a format to be used for install_data
        def walk_helper(arg, dirname, files):
            if '.svn' in dirname:
                return
            names = []
            lst, wildcards = arg
            for wc in wildcards:
                wc_name = self.opj(dirname, wc)
                for f in files:
                    filename = self.opj(dirname, f)
 
                    if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
                        names.append(filename)
            if names:
                lst.append( (dirname, names ) )
 
        file_list = []
        recursive = kw.get('recursive', True)
        if recursive:
            os.path.walk(srcdir, walk_helper, (file_list, wildcards))
        else:
            walk_helper((file_list, wildcards),
                        srcdir,
                        [os.path.basename(f) for f in glob.glob(self.opj(srcdir, '*'))])
        return file_list
 
    def run(self):
        if os.path.isdir(self.dist_dir): #Erase previous destination dir
            shutil.rmtree(self.dist_dir)
       
        #Use the default pygame icon, if none given
        if self.icon_file == None:
            path = os.path.split(pygame.__file__)[0]
            self.icon_file = os.path.join(path, 'pygame.ico')
 
        #List all data files to add
        extra_datas = []
        for data in self.extra_datas:
            if os.path.isdir(data):
                extra_datas.extend(self.find_data_files(data, '*'))
            else:
                extra_datas.append(('.', [data]))
       
        setup(
            cmdclass = {'py2exe': pygame2exe},
            version = self.project_version,
            description = self.project_description,
            name = self.project_name,
            url = self.project_url,
            author = self.author_name,
            author_email = self.author_email,
            license = self.license,
 
            # targets to build
            windows = [{
                'script': self.script,
                'icon_resources': [(0, self.icon_file)],
                'copyright': self.copyright
            }],
            options = {'py2exe': {'optimize': 2, 'bundle_files': 1, 'compressed': True, \
                                  'excludes': self.exclude_modules, 'packages': self.extra_modules, \
                                  'dll_excludes': self.exclude_dll,
                                  'includes': self.extra_scripts} },
            zipfile = self.zipfile_name,
            data_files = extra_datas,
            dist_dir = self.dist_dir
            )
       
        if os.path.isdir('build'): #Clean up build dir
            shutil.rmtree('build')
 
if __name__ == '__main__':
    if operator.lt(len(sys.argv), 2):
        sys.argv.append('py2exe')
    BuildExe().run() #Run generation


and then just a regular script running importing pygame, which i didnt think you may need.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1388
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] compiling an exe

Postby metulburr » Mon May 13, 2013 1:35 am

ok i guess that was an easy fix.

not sure what this is for but whatever, it worked.

added to the main source file
Code: Select all
import pygame._view
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1388
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] compiling an exe

Postby Mekire » Mon May 13, 2013 8:24 am

There are lots of stupid things that always stop pygame from working with py2exe. (never use a sysfont for example).

This has served me pretty well in the past:
Code: Select all
#Note that you may also have to add the line import pygame._view to
#your main script as well.
from distutils.core import setup
import py2exe, sys, os

origIsSystemDLL = py2exe.build_exe.isSystemDLL # save the orginal before we edit it
def isSystemDLL(pathname):
    # checks if the freetype and ogg dll files are being included
    if os.path.basename(pathname).lower() in ("libfreetype-6.dll", "libogg-0.dll", "sdl_ttf.dll"):
            return 0
    return origIsSystemDLL(pathname) # return the orginal function
py2exe.build_exe.isSystemDLL = isSystemDLL # override the default function with this one

sys.argv.append('py2exe')

#Replace script with your file name:
setup(options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
      windows = [{'script': "Your_Main_File_Name_Here.pyw"}],
      zipfile = None)

The settings above will bundle all the garbage into your exe too, so you don't have a directory full of crap.
-Mek

Edit: Noticed this is pretty much the same as the one you found but a little more cut and dry.
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] compiling an exe

Postby metulburr » Mon May 13, 2013 9:43 am

oh nice.

i believe py2exe was not yet ported to python3.x? I use cx_freeze as a general base as i wanted 3.x support. But i cant find an option to bundle up the files like that as it does in py2exe?

this is the script that i found and modified:
Code: Select all
import sys
from cx_Freeze import setup, Executable

includefiles = []

#if sys.platform.startswith('win'):
# includefiles.append( ("win32\\dll", ".") ) # my custom dlls are there

options = {
#"icon": 'win32/citronicon.ico',
#"include_files": includefiles
}

mybase = None
if sys.platform.startswith('win'):
    mybase = "Win32GUI"

cible_1 = Executable(
    script = "game.py",
    base = mybase,
    compress = True,
   copyDependentFiles = True,
)
   
setup(
   name = "main",
   version = "0.4",
    description = u"game name",
    author = "me",
   options = {'build_exe':options},
   executables = [cible_1]
 )
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1388
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] compiling an exe

Postby Mekire » Mon May 13, 2013 10:40 am

I find cx-freeze is just all around easier to get working; but no, I have searched around and never found a way to bundle files with it. It creates a ton of garbage too =/.

I'm not sure if there is a py3 specific version of py2exe; I suspect not. I always try to make sure that my pygame code can run on both 2 and 3 though so I never have a problem using the 2.x version. What are you doing that is forcing you to use 3?

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

Re: [Pygame] compiling an exe

Postby octopez » Mon May 13, 2013 12:11 pm

I'm not sure if there is a py3 specific version of py2exe;

AFAIK not. Py2Exe has never been ported, so you have to use cxFreeze for Python 3. I find cxFreeze easier too. :)
But: I've got a Pygame related book (from Author Andy Harris, called "Game Programming: The L Line , The Express Line to Learning") which has an extra chapter regarding py2exe. I think he suggest a "tweaked" version of Py2exe that is specially made for Pygame IIRC. I'll have a look and report back.
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: [Pygame] compiling an exe

Postby metulburr » Mon May 13, 2013 1:50 pm

What are you doing that is forcing you to use 3?

nothing. I just find the 3.x method if possible and use that unless i am restricted to 2.x
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1388
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] compiling an exe

Postby Mekire » Mon May 13, 2013 1:54 pm

I just find it is hard enough to get people to check out my projects as is so I try to keep them as compatible as possible. When I first started python I did everything in 3 and originally my first game was 3 only. Hardly anyone downloaded it =/.

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

Re: [Pygame] compiling an exe

Postby metulburr » Mon May 13, 2013 3:09 pm

When I first started python I did everything in 3 and originally my first game was 3 only. Hardly anyone downloaded it =/.


interesting.

I will take that into consideration when writing the code
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1388
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] compiling an exe

Postby octopez » Tue May 14, 2013 8:26 am

me wrote:I'll have a look and report back.

The chapter about distributing modules and programs is downloadable Appendix.
The PDF can be on the book's official site:

http://media.wiley.com/product_ancillar ... ndix_C.pdf

I just took a look - I was wrong. No tweaked version of py2exe, but a nice explanation how to use it. Maybei this helps a little!
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: [Pygame] compiling an exe

Postby Mekire » Tue May 14, 2013 8:38 am

The "tweaked version of py2exe" generally just refers to the snippets both me and Metul posted previously. They are just hacks to get around some stupid issues that arise regarding font compatibility and dlls. Unfortunately despite how dumb the problems are, the fixes aren't all that trivial for a first time user. Sometimes these fixes are reffered to as pygame2exe, but this is a little bit of an exageration:

http://www.pygame.org/wiki/Pygame2exe

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


Return to General Coding Help

Who is online

Users browsing this forum: feneyes and 2 guests