0

I managed to install Pygame on Big Sur. but when I use modules within it, it fails out. I have a new M1 laptop running Big Sur (11.4) and I installed homebrew, python3, pip3, and then Pygame.

If I run this skeleton:

import sys

import pygame
from pygame.locals import *

pygame.init()

fps = 60
fpsClock = pygame.time.Clock()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))

is_blue = True

# Game loop.
while True:
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            is_blue = not is_blue

    # Update.

    # Draw.
    if is_blue: color = (0, 128, 255)
    else: color = (255, 100, 0)
    pygame.draw.rect(screen, color, pygame.Rect(30, 30, 60, 60))

    pygame.display.flip()
    fpsClock.tick(fps)

it works fine. But if I add this line just after init:

myfont1 = pygame.font.SysFont("Comic Sans MS", 20)

I get the following error message:

pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
/Users/lmiratrix/PycharmProjects/pythonProject1/main_v2.py:8: RuntimeWarning: use font: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/opt/homebrew/lib/python3.9/site-packages/pygame/font.py)
(ImportError: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/opt/homebrew/lib/python3.9/site-packages/pygame/font.py))
  myfont1 = pygame.font.SysFont("Comic Sans MS", 20)
Traceback (most recent call last):
  File "/Users/lmiratrix/PycharmProjects/pythonProject1/main_v2.py", line 8, in <module>
    myfont1 = pygame.font.SysFont("Comic Sans MS", 20)
  File "/opt/homebrew/lib/python3.9/site-packages/pygame/__init__.py", line 59, in __getattr__
    raise NotImplementedError(missing_msg)
NotImplementedError: font module not available (ImportError: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/opt/homebrew/lib/python3.9/site-packages/pygame/font.py))

I am not sure how to proceed (and I am hopeful this is not the cosmos telling me comic sans is just a no go! :-) )

Background: I really struggled to get Pygame to work in the first place, ending up with strange error messages in PyCharm. I shifted to terminal after figuring out that homebrew is installing everything, including a fresh python3, in /opt/homebrew/ rather than /usr/local/bin or similar, but PyCharm was only finding python installations in /usr/bin for a bit. I tried to uninstall Pygame and then used spotlight to delete all the various Pygame files from the computer, reinstalled everything, and then landed here with a partial success.

miratrix
  • 191
  • 2
  • 12
  • Did you try using `pip`? Or, in any way, is the module downloader you use outdated? I know that, for example, older `pip` versions can only download PyGame 1.9.6. – D_00 Jun 27 '21 at 19:52
  • I am using pip3 to install pygame. In the error message I think it is telling me I am using 2.0.1? – miratrix Jun 27 '21 at 21:07

1 Answers1

2

According to these two github issues (#2150, #2500), this problem could arise for a couple of reasons.

First, I see that you're running Python 3.9.5, and one issue says that running newer versions of python with pygame on some machines may cause issues, so try using and older version of python (like 3.7 or 3.8).

Second, you may be getting this error because of incompatibility issues with pygame. One of the above issues mentioned pygame incompatibility with a M1 Mac, an I expect similar issues to arise on a Big Sur install. In one of the above github issues, this problem was solved by using pygame version 2.0.0, which you can get using pip install pygame==2.0.0.dev6 --no-cache-dir.

Your error message also mentioned circular imports, so make sure you don't have multiple files importing each other. If you still have errors, please look at the linked github issues to find more solutions.

abnathan
  • 121
  • 1
  • 4
  • Being lazy, I tried the easier change of pygame (#2, above) without reinstalling python itself. It mostly worked! In particular, my kid and I now can access the modules, but loading images only works for BMP (i.e., we get the error discussed here: https://stackoverflow.com/questions/28194070/pygame-error-file-is-not-a-windows-bmp-file). We will tackle this later, after we recover from this round of installation woes. Thanks so much! – miratrix Jun 29 '21 at 15:13