My environment:
Windows 10
Python 3.11.1
Pygame2.2.0
I installed Pygame with pip and install was successful.
pip install pygame
If I run IDLE and type into the console “import pygame” everything is fine, and I’m able to follow up with “pygame.init()” in console and initialization runs successfully - outputs (5,0).
BUT… if I then try to run a script that attempts to import pygame, I get this error in the console:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import pygame
File "[path]\pygame.py", line 6, in <module>
pygame.init()
AttributeError: partially initialized module 'pygame' has no attribute 'init' (most likely due to a circular import)
If I then try to type “import pygame” in the console, this error remains and I’d have to restart IDLE for all this to repeat again.
Is this a known issue or is there something wrong with the way pygame was installed?
(I understand the basic idea of circular import errors, but seeing as I didn’t modify any directory or paths for pygame I’m not sure why this is happening, unless it’s something within pygame itself.)
In an IDLE editor, I saved the following as pygame.py.
import pygame
pygame.init()
When I ran it, I got the following traceback, very similar to yours.
Traceback (most recent call last):
File "F:/dev/tem/pygame.py", line 1, in <module>
import pygame
File "F:\dev/tem\pygame.py", line 2, in <module>
pygame.init()
AttributeError: partially initialized module 'pygame' has no attribute 'init' (most likely due to a circular import)
I don’t suppose you’ve named your own script pygame.py? Don’t do that!
Python includes the directory of the script in the module search path,
so now your script imports… itself when you import pygame.
If that’s not what’s going on, please provide more details.
It’s the pygame.init() in the ...../pygame.py file which makes me
suspect this, BTW.
The traceback in the initial post is not the traceback OP got when running from the file but when OP typed “import pygame” in the console. At this point, the current working directory for user code would have been the directory of OP’s file. I don’t know why the ‘[path]’ prefix.
I made a test.py and the file contains only “import pygame” and I’m still getting the same error
Traceback (most recent call last):
File "C:\Users\usr\Documents\Python\test.py", line 6, in <module>
import pygame
AttributeError: partially initialized module 'pygame' has no attribute 'init' (most likely due to a circular import)
OK, perhaps not a duplicate name. In IDLE, did you run with standard F5? Please run the same file experiment without IDLE. py C:\Users\usr\Documents\Python\test.py
Please read the previous responses carefully. You likely still have a file pygame.py within the same directory as test.py, which is imported (instead of the real pygame package) when you run import pygame in test.py. This in turn executes the module level code in pygame.py, namely import pygame, which triggers the error because the module is trying to import itself. You need to delete or rename this pygame.py file to solve the problem.