Pygame and PyInstaller

28 views
Skip to first unread message

Vincent Le Goff

unread,
Jun 20, 2019, 11:42:45 AM6/20/19
to PyInstaller
Hello,


I'm a new user of PyInstaller and appreciate its simplicity.  I have,
however, noticed a problem when using pygame.


pygame is an interesting Python library to create games.  It handles
basic operations (like a simple 2D window, sound output, keyboard
processing and so on).  When trying to make an executable out of a game
using pygame, everything seems to work... but things break when pygame
attempts to load its fonts.  Here's the end of the traceback I receive
on calling pygame.font.Font (this is in the executable, not the original
Python script):


  File "site-packages\pygame\pkgdata.py", line 50, in getResource
  File "site-packages\pkg_resources\__init__.py", line 1134, in
resource_exists
  File "site-packages\pkg_resources\__init__.py", line 1404, in
has_resource
  File "site-packages\pkg_resources\__init__.py", line 1464, in _has
NotImplementedError: Can't perform this operation for unregistered
loader type


(I am running the pyinstaller script in a virtualenv, which might also
play a role.)


4The problem doesn't seem to come from pygame directly.  I only have the
slightest idea of what pkg_resources actually does.  Does it sound
familiar to you?  And how to fix it?


Thanks for your help,


Vincent

Raoul Snyman

unread,
Jun 20, 2019, 8:36:20 PM6/20/19
to PyInstaller
Hi Vincent,

I was just browsing the PyInstaller documentation the other day
looking for something unrelated, but I came across this question in
the FAQ:

What about pkg_resources?
pkg_resources is currently not supported by PyInstaller. This means
that an application using a library which uses the the pkg_resources
API will probably not work out of the box. The only situation in which
it works is when it's being used on .egg files (see above). For
details follow issue #183.

See https://github.com/pyinstaller/pyinstaller/wiki/FAQ

It may be that PyGame is trying to load something via pkg_resources?

I'm sorry I can't be of more help, I'm not very familiar with pygame
or pkg_resources.
> --
> You received this message because you are subscribed to the Google Groups "PyInstaller" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
> To post to this group, send email to pyins...@googlegroups.com.
> Visit this group at https://groups.google.com/group/pyinstaller.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/4e5a8c6f-c93e-e712-2583-b0da68332a2a%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Raoul Snyman
B.Tech Information Technology (Software Engineering)
E-Mail: raoul....@gmail.com
Web: http://www.saturnlaboratories.co.za/
Blog: http://blog.saturnlaboratories.co.za/
Mobile: 082 550 3754
Registered Linux User #333298 (http://counter.li.org)

Vincent Le Goff

unread,
Jun 30, 2019, 5:26:37 AM6/30/19
to pyins...@googlegroups.com
Okay, here's my temporary fix:

1. Add a default font file in your game.  My choice is the
'freesansbold.ttf' as this one does seem standard and is included in
Pygame (you'll find it in its source code, if nothing else, or
site-packages/pygame/freesansbold.ttf).
2. Add a few line before creating the font.

import sys

if getattr(sys, "frozen"): # FROZEN executable
    font_family = "./freesansbold.ttf" # Hard-code a local path
else:
    font_family = pygame.font.match_font(font_family)
self.font_object = pygame.font.Font(font_family, font_size)

So we give it the local filename if the executable is frozen. Copy the
freesansbold.ttf next to the final executable and try it.  It should
work.  Wrap the data file is you like to do so, it all should work. 
This is, however, a temporary fix, it would be great to have
pkg_resources working, but one step at a time.

Cheers,

Vincent

Vincent Le Goff

unread,
Jun 30, 2019, 5:27:12 AM6/30/19
to pyins...@googlegroups.com
Thank you, it was most helpful.  Unfortunately, the linked issue is
closed and doesn't seem to have as much connection to the problem as I'd
like (or an open issue on the PyInstaller tracker itself).  I'm
wondering if this will be "fixed", in any way, or if we should live with
it.  In my case, the reason why pkg_resources is needed is to load fonts
(a .tif file if I'm not mistaken) so pygame cannot find it and
PyInstaller probably doesn't.  But perhaps there's a way around it, to
include the .tif file somewhere else in a data file... I'll look into that.


In any case, thank you for the answer, I have a better idea of what's
going on.


Vincent

On 6/21/2019 2:36 AM, Raoul Snyman wrote:
Reply all
Reply to author
Forward
0 new messages