0

i made a py2exe executable, it's a "Programming Quiz". It is made in pygame and when I run it as an EXE, it all works until the end. I assume because the end has pygame text in it. The error is below. Here's the portion of the code that doesn't work as an exe but as a normal .py:

def endgame():
    global programmer
    if programmer < 0:
        programmer = 0
    font = pygame.font.SysFont(None, 25)
    text = font.render("You are: " + str(programmer) + "% a programmer.", True, black)
    gameDisplay.blit(text, (170,200))

Error:

C:\Python27\Programming Survey\dist>survey.exe
survey.exe:43: RuntimeWarning: use font: DLL load failed: The specified module c
ould not be found.
(ImportError: DLL load failed: The specified module could not be found.)
Traceback (most recent call last):
  File "survey.py", line 223, in <module>
  File "survey.py", line 217, in main
  File "survey.py", line 43, in endgame
  File "pygame\__init__.pyc", line 70, in __getattr__
NotImplementedError: font module not available
(ImportError: DLL load failed: The specified module could not be found.)
HKVariant
  • 837
  • 10
  • 23
  • 1
    One way to debug stuff like this is to run the .exe from a console. And, if that doesn't work and it just creates a new console instead, just use a `try:/finally:` around the main code to have it `input()` at the end so you get a chance to see what it's printing out before the window closes. – abarnert Dec 06 '14 at 00:17
  • Thanks, error posted – HKVariant Dec 06 '14 at 00:22
  • Where did you get PyGame from, and how did you install it? – abarnert Dec 06 '14 at 01:20
  • I got it from the main site and installed it like normal. Works as a .py file. – HKVariant Dec 06 '14 at 11:49

2 Answers2

0

OK, your problem is that you're trying to use pygame.font, but your installation of PyGame doesn't include that module.

As the docs say:

This module is optional and requires SDL_ttf as a dependency. You should test that pygame.font is available and initialized before attempting to use the module.

There are actually two reasons it could fail to load:

  1. You didn't install SDL_ttf before building PyGame (or explicitly disabled it, or installed a pre-compiled binary that did one of these), so the support isn't even built in to your PyGame.
  2. You built against SDL_ttf as a DLL, but that DLL isn't available at runtime.

Either way, that means you can't use pygame.font.* If your problem is #1, you will need to rebuild PyGame (or get a different binary installation). If it's #2, you will need to install SDL_ttf.dll the same way you installed the other SDL DLLs.

If you're not sure, try #2 first, and, if it still doesn't work, you need a new build of PyGame.

If you know what you're doing (or just think maybe whoever built your binary may have included support for it instead of pygame.font), you may want to consider using pygame.freetype (which has a slightly different interface) or pygame.ftfont (a nearly complete drop-in replacement for pygame.font build on pygame.freetype) instead. You may even want to os.putenv('PYGAME_FREETYPE', '1') or similar, which makes newer versions of pygame.font try pygame.ftfont as a fallback. But all of this is much more likely to be relevant to Linux users than Windows users. Also note that it's not included in standard builds until 1.9.2, and as of 5 Dec 2014 they're still not shipping official releases of 1.9.2.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Well it works as a normal .py file just so you know. I have tried putting the SDL_ttf.dll file in the dist folder, no dice. – HKVariant Dec 06 '14 at 11:48
  • It just says that the dll failed to load in the .exe file form, which is confusing. – HKVariant Dec 06 '14 at 12:21
  • @HunterKepley: Then the problem is that the DLL isn't getting bundled into the right place by `py2exe`. This can be fiddly to get right, but if you don't get it right, you're going to have this problem. – abarnert Dec 08 '14 at 22:08
0

Fixed by using pygame2exe instead, it uses py2exe as well, but it has the font dlls and such preloaded so it worked.

HKVariant
  • 837
  • 10
  • 23