I just learned how to use py2exe to turn a helloworld.py script into a helloworld.exe script. But when I do it for something more complicated, I get an error. The exe is created, but when I run it I get an error:

C:\Documents and Settings\Gulshan\My Documents\Python Scripts\dist>snake.exe
snake.exe:85: RuntimeWarning: use font: DLL load failed: The specified module co
uld not be found.
Traceback (most recent call last):
File "Snake.py", line 85, in <module>
File "pygame\__init__.pyc", line 70, in __getattr__
NotImplementedError: font module not available

How do I fix this?

I downloaded Gpy2exe, and it was definitely easier to use but I kept getting the error "This application has failed to start because MSVCR90.dll was not found. Re-installing the application may fix this problem." I don't know how to fix this...

it looks like you are missing that particular library on your machine. i think you will probably be able to download a copy of MSVCR90.dll (google) and place it into your libraries folder, which generally is C:\WINDOWS\system32\

I did that and that error is fixed, but now it says "the application failed to initialize properly(some random number)". How about I paste the source code and someone compiles it and sees the error for themselves?

#! /usr/bin/env python

# Worm Game

import pygame
import random

# Constants

UP = (0, -1)
RIGHT = (1, 0)
DOWN = (0, 1)
LEFT = (-1, 0)

# Worm Class

class Worm:

    def __init__(self, surface, length, x, y):
        self.surface = screen
        self.length = length
        self.x = x
        self.y = y
        self.hdir = 0
        self.vdir = -1
        self.body = []
        self.crashed = False
        self.eat = False

    def direction(self, event):
        if event.key == pygame.K_UP and self.vdir != 1:
            self.hdir, self.vdir = UP
        if event.key == pygame.K_RIGHT and self.hdir != -1:
            self.hdir, self.vdir = RIGHT
        if event.key == pygame.K_DOWN and self.vdir != -1:
            self.hdir, self.vdir = DOWN
        if event.key == pygame.K_LEFT and self.hdir != 1:
            self.hdir, self.vdir = LEFT

    def move(self):
        self.x += self.hdir
        self.y += self.vdir

        r, g, b, a = self.surface.get_at((self.x, self.y))
        
        if (r, g, b) == (255, 255, 255) or w.x > width-2 or w.x < 1 or w.y > height-2 or w.y < 1:
            self.crashed = True

        if (r, g, b) == (0, 255, 0):
            self.eat = True

        self.body.insert(0, (self.x, self.y))

        if len(self.body) > self.length:
            self.body.pop()

    def draw(self):
        for x, y in self.body:
            self.surface.set_at((x, y), (255, 255, 255))

# Food Class

class Food:

    def __init__(self, surface, x, y):
        self.x = x
        self.y = y
        self.surface = surface

    def draw(self):
        self.surface.set_at((self.x, self.y), (0, 255, 0))

    def change(self):
        self.surface.set_at((self.x, self.y), (0, 0, 0))
        self.x = random.randrange(10, width-10)
        self.y = random.randrange(10, height-10)
        self.draw()

# Screen

width = 640
height = 400

pygame.init()
pygame.font.init()

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake")

# Text code

def drawtext(score):
    font = pygame.font.Font(None, 30)
    thescore = "Score: "+str(score)
    heading = "How many can ya get before ya give up?"
    text = font.render(thescore, True, (255, 255, 255), (0, 0, 0))
    title = font.render(heading, True, (255, 255, 255), (0, 0, 0))
    pygame.draw.line(screen, (255,255,255), (0, 30), (640, 30))
        
    screen.blit(text, (5, 5))
    screen.blit(title, (140, 5))

clock = pygame.time.Clock()
running = True
score = 0

# Worm Instance

w = Worm(screen, 100, width/2, height/2)
f = Food(screen, random.randrange(10, width-10), random.randrange(40, height-10))

# Event Loop

while running:
    screen.fill((0, 0, 0))
    drawtext(score)

    f.draw()

    w.draw()
    w.move()

    if w.eat == True:
        f.change()
        w.eat = False
	score += 1
	w.length += 50

    if w.crashed == True:
        print "Crashed!"
        break
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            w.direction(event)

    pygame.display.flip()
    clock.tick(120)

Any luck?

The problem is with the line:
font = pygame.font.Font(None, 30)

This is asking for a default font - although freesansbold.ttf
is the default font Py2exe doesn`t seem to work very well with this way of asking for it.

replace the above line with something more specific like:
font = pygame.font.Font("freesansbold.ttf", 30)

and remember to include a copy of freesansbold.ttf in the folder where you make the exe and the dist folder.

your setup.py should look something like this:

from distutils.core import setup
import py2exe
setup(
windows = [{"script":"worm.py"}],
data_files = [ (".", ["freesansbold.ttf"]) ]
)


I hope you understand this.

(By the way, although I know you dont have any audio in this program it is worth mentioning that py2exe also doesnt seem to work very well with the pygame.mixer.Music module so instead always use pygame.mixer.Sound - this information should be useful later).

First of all thanks for replying even though the thread is old.

Second of all, I did everything you said (except I used arial.ttf) and I got the same error. I used the exact same setup.py file except with the changed font, like i said..

The actual font you use doesn`t matter provided you specify it in the code in the way I showed.

I only have two suggestions: firstly, make sure you have a copy of the Arial.TTF :
a ) in the directory where you`ve installed python
b) in the folder where you put your setup.py and your worm.py and c) in the dist folder that is created ( before you run the exe (of course, only if an exe is created).

Secondly, I wouild suggest uninstalling the py2exe you have and downloading and installing it again. That worked for me once.

Hope this is of help.

I got it to work simply giving the full path of the font file ...

font = pygame.font.Font("C:/Windows/Fonts/comic.TTF", 30)

For py2exe and pygame I often use this modified script ...

# py2exe setup program for pygame
# many options removed, no custom icon
# tested with Python25  by vegaseat

from distutils.core import setup
import py2exe
import pygame
import numpy
import sys
import os
import shutil
sys.argv.append("py2exe")

# your sript file should be in the working directory
SCRIPT_MAIN = 'PG_WormGame5.py'

if os.path.exists('dist/'): shutil.rmtree('dist/')

# list of all modules to exclude from the distribution build
# gets rid of modules unnecessary for proper functioning of app
# only put things in this list you know aren't needed
# reduces the size of your distribution by about 2M bytes
MODULE_EXCLUDES = [
'email',
'AppKit',
'Foundation',
'bdb',
'difflib',
'tcl',
'Tkinter',
'Tkconstants',
'curses',
'distutils',
'setuptools',
'urllib',
'urllib2',
'urlparse',
'BaseHTTPServer',
'_LWPCookieJar',
'_MozillaCookieJar',
'ftplib',
'gopherlib',
'_ssl',
'htmllib',
'httplib',
'mimetools',
'mimetypes',
'rfc822',
'tty',
'webbrowser',
'socket',
'hashlib',
'base64',
'compiler',
'pydoc'
]

INCLUDE_STUFF = ['encodings', 'encodings.latin_1']

# the stage is set for setup() ...
setup(
  windows = [
    {'script': SCRIPT_MAIN
    }
  ],
  options = {
    "py2exe":
    { "optimize": 2,
    "includes": INCLUDE_STUFF,
    "compressed": 1,
    "ascii": 1,
    "bundle_files": 1,  # 1 includes python.dll into .exe file
    "ignores": ['tcl', 'AppKit', 'Numeric', 'Foundation'],
    "excludes": MODULE_EXCLUDES
    }
  },
  zipfile = None  # no common zipfile, everything in .exe file
)

In answer to the previous question. No, I didn`t download
MSVC2008 C++ runtime DLL - although whether this is automatically on my computer, or not I don`t know.

I haven`t tried Gpy2exe nor GUI2exe, however, py2exe works fine for me ( including making your program into an exe ) - so I`ve had to reason to try anything else.

In answer to the previous question. No, I didn`t download
MSVC2008 C++ runtime DLL - although whether this is automatically on my computer, or not I don`t know.

I haven`t tried Gpy2exe nor GUI2exe, however, py2exe works fine for me ( including making your program into an exe ) - so I`ve had to reason to try anything else.

THe reason i suggested that you download MSVC files is due to the fact that PyQt also suggests that.

I was having the same trouble with MSVCP90.DLL file. Py2exe needed that .DLL when converting a python file that Imports wxPython. I found the .DLL file on my computer in the Windows/WinSxS folder. Copied to the Windows/System32 folder as suggest. Fixed my problem.
If I'm correct, I think the WinSxS folder was created when I installed Microsoft Visual C++ 2008 as recommended.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.