Pygame: Font module not available

2019-07-20 11:40发布

问题:

Running Python 2.7.2, and Pygame 1.9.2pre on OS X.

Another noobious issue: Initialising the pygame.font module produces an error that I dare not... well, attempt to solve myself. I'm guessing this is a rather generic pygame-related issue... but I haven't been able to find any solutions.

import pygame
pygame.init()

pygame.font.init()

Gives:

Desktop/font.py:4: RuntimeWarning: use font: dlopen(/Library/Python/2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
  Reason: image not found
(ImportError: dlopen(/Library/Python/2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
  Reason: image not found)
  pygame.font.init()
Traceback (most recent call last):
  File "Desktop/font.py", line 4, in <module>
    pygame.font.init()
  File "/Library/Python/2.7/site-packages/pygame/__init__.py", line 70, in __getattr__
    raise NotImplementedError(MissingPygameModule)
NotImplementedError: font module not available
(ImportError: dlopen(/Library/Python/2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
  Reason: image not found)

What exactly is wrong here? What are some possible reasons that the font module could not be available? Common causes? Common fixes?

If anyone can enlighten me about what's wrong, I'll appreciate it even if it doesn't solve the problem! Thanks very much!

Edit: I am using 64 bit version of Python with 32 bit Pygame. (There seems to be no 64 bit Pygame available for Mac OS)

回答1:

(self answer)

Problem: Using 32-bit Pygame with 64-bit Python generally isn't a good idea.

Solution: Always use Python and Pygame both in 64-bit architecture. (Or 32).

P.S. Currently there doesn't seem to be a 64-bit Pygame for OS X, so you'll have to use a 32-bit Python.

P.S.S (After installing a 32-bit Python you may need to re-install the 32-bit Pygame for it to have any effect. This is what happened to me)



回答2:

I encounter the same problem, and the making-this-problem progress is as follow:

  1. I download the pygame source file from the http://www.pygame.org/
  2. I install the pygame with command python setup.py install

Then, I can import pygame and use basic pygame functions normally, but if I try to import other relative modules like import pygame.font, it jumps out font module not found error. I try to fix this problem by install some missing modules manually as suggested here:

$ sudo apt-get install python-dev libsdl-image1.2-dev libsdl-mixer1.2-dev  
libsdl-ttf2.0-dev   libsdl1.2-dev libsmpeg-dev python-numpy subversion 
libportmidi-dev ffmpeg libswscale-dev libavformat-dev libavcodec-dev

However, this problem still exists.

At last, I realize this is a dependency problem, so I remove installed pygame by the command sudo python setup.py clean and remove all files in python/site-packages. I reinstall the pygame by the command:

sudo apt-get install python-pygame

and everything works fine.