Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'Font' from partially initalized module 'pygame.font' (most likely due to a circular import) #4170

Open
neuron-whisperer opened this issue Mar 7, 2024 · 1 comment
Labels

Comments

@neuron-whisperer
Copy link

neuron-whisperer commented Mar 7, 2024

Environment:

You can get some of this info from the text that pops up in the console when you run a pygame program.

  • Operating system (e.g. Windows, Linux(Debian), Linux(Ubuntu), Mac): Linux Bullseye Lite
  • Python version (e.g. 3.7.9, 3.8.5) : 3.12.2
  • SDL version (e.g. SDL 2.0.12): SDL 2.0.14
  • PyGame version (e.g. 2.0.0.dev10, 1.9.6): 2.5.2
  • Relevant hardware (e.g. if reporting a bug about a controller, tell us the brand & name of it): Raspberry Pi Zero 2W

Current behavior:

Attempting to import pygame.font causes a fatal exception with this message:

 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/pi/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pygame/font.py", line 17, in <module>
     from pygame.sysfont import match_font, get_fonts, SysFont as _SysFont
   File "/home/pi/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pygame/sysfont.py", line 27, in <module>
     from pygame.font import Font
 ImportError: cannot import name 'Font' from partially initialized module 'pygame.font' (most likely due to a circular import) (/home/pi/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pygame/font.py)

Looking into the indicated modules indeed reveals a circular import problem.

In font.py:

 """pygame module for loading and rendering fonts (freetype alternative)"""

...line 17:

 from pygame.sysfont import match_font, get_fonts, SysFont as _SysFont

In sysfont.py, line 27:

 from pygame.font import Font

Expected behavior:

Importing pygame.font should not cause a circular import error.

Screenshots

n/a

Steps to reproduce:

Please explain the steps required to duplicate the issue, especially if you are able to provide a sample application.
if the bug is caused by a specific file (image, font, sound, level, please upload it as an attachment

  1. Install Raspberry Pi (Bullseye) Lite
  2. Install Python via pyenv
  3. Install pygame via pip (builds from source)
  4. Run python and import pygame.font

Test code

import pygame
import pygame.font
@RaphyStark
Copy link

RaphyStark commented Mar 24, 2024

Hi, I also have a problem when trying to use "font" :

Python 3.6.9 (default, Mar 10 2023, 16:46:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
_frozen_importlib:219: RuntimeWarning: Your system is avx2 capable but pygame was not built with support for it. The performance of some of your blits could be adversely affected. Consider enabling compile time detection with environment variables like PYGAME_DETECT_AVX2=1 if you are compiling without cross compilation.
/home/raphael/projects/robotNav_PROJECT/robotNavVenv/lib/python3.6/site-packages/pygame-2.6.0.dev1-py3.6-linux-x86_64.egg/pygame/pkgdata.py:25: UserWarning: Module pygame was already imported from /home/raphael/projects/robotNav_PROJECT/robotNavVenv/lib/python3.6/site-packages/pygame-2.6.0.dev1-py3.6-linux-x86_64.egg/pygame/__init__.py, but /home/raphael/projects/robotNav_PROJECT/pygame is being added to sys.path
  from pkg_resources import resource_stream, resource_exists
pygame 2.6.0.dev1 (SDL 2.0.8, Python 3.6.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
(2, 0)
>>> font = pygame.font.SysFont(None, 36)
__main__:1: RuntimeWarning: use font: cannot import name 'Font'
(ImportError: cannot import name 'Font')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/raphael/projects/robotNav_PROJECT/robotNavVenv/lib/python3.6/site-packages/pygame-2.6.0.dev1-py3.6-linux-x86_64.egg/pygame/__init__.py", line 70, in __getattr__
    raise NotImplementedError(missing_msg)
NotImplementedError: font module not available (ImportError: cannot import name 'Font')

I first installed pygame via pip, then uninstalled it and followed the https://www.pygame.org/wiki/CompileUbuntu instructions when using virtual environment (but I use python venv instead of mkvirtualenv).

Looked for other related issues but didn't find a solution.
#2150
#2500
https://stackoverflow.com/questions/20842801/how-to-display-text-in-pygame
https://stackoverflow.com/questions/17823923/pygame-font-module-not-available
https://forums.fedoraforum.org/showthread.php?289229-Pygame-no-font

pygame.freetype doesn't work neither since it use font.

Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants