IndexAboutWeblogRecipesProjectsLinksSearchRSS

Categories

ThadeusB python, ai, robots

pygame font and py2exe

If you get the errors at the bottom of this post while trying to use pygame.font.Font or pygame.font.SysFont and py2exe here is a simple fix.

Be sure to include 'pygame' as a package to your py2exe script. Also you will need to copy SDL.dll and SDL_ttf.dll from the pygame directory. If you use the default python font you will also need to include that as well.

I have been wrecking my brain the last two and a half days trying to solve this problem.

In my last attempt at solving the problem, I figured "Heck, it won't hurt to grab every DLL from system32, pygame and what ever else I could find and put them in my dist directory". I almost cried with joy when it worked! I narrowed it down to SDL.dll and SDL_ttf.dll are the magick files.

Is this a bug that py2exe does not find these and include them automatically? Using optimize 2, bundle_files 1, nozip

Helpful code:

pygamedir = os.path.split(pygame.base.__file__)[0]
os.path.join(pygamedir, pygame.font.get_default_font()),
os.path.join(pygamedir, 'SDL.dll'),
os.path.join(pygamedir, 'SDL_ttf.dll')

Relative Tracebacks: MemoryLoadLibrary

Traceback (most recent call last):
File "myrts.py", line 1, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "scene.pyo", line 23, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "world.pyo", line 31, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "tileengine.pyo", line 32, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "widgets.pyo", line 18, in <module>
File "zipextimporter.pyo", line 98, in load_module
ImportError: MemoryLoadLibrary failed loading pygame

ont.pyd

DLL load failed

Traceback (most recent call last):
File "myrts.py", line 1, in <module>
File "scene.pyo", line 23, in <module>
File "world.pyo", line 31, in <module>
File "tileengine.pyo", line 32, in <module>
File "widgets.pyo", line 18, in <module>
File "pygame

ont.pyo", line 12, in <module> File "pygameont.pyo", line 10, in __load ImportError: DLL load failed: The specified module could not be found.

NotImplementedError font module not available

MyRTSdistMy RTS.exe:68: RuntimeWarning: use font: MemoryLoadLibrary failed loading pygame/f

ont.pyd Traceback (most recent call last):File "myrts.py", line 106, inFile "myrts.py", line 75, in runFile "scene.pyo", line 97, in loadFile "widgets.pyo", line 107, in init #loads the pygame.font module here.File "pygameinit.pyo", line 70, in getattr NotImplementedError: font module not available

Note: For some reason py2exe regards the SDL_ttf.dll file as a system owned dll and excludes it from the distribution package. That is why this error is being received.

To override this you can use a trick found in py2exe's FAQ. Like this:

origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
       if os.path.basename(pathname).lower() in ["sdl_ttf.dll"]:
               return 0
       return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL

This will make sure that the SDL_ttf.dll isn't recognized as a system dll.