Search code examples
pythonperformancepygamepygame2

What would be causing pygame.init() to take 40 seconds to execute?


Pygame was working perfectly last night, now the pygame.init() function is taking ~ 40 seconds to finish compared to being instant before.

import pygame
import time
start = time.time()
pygame.init()
print(f"Runtime: {time.time() - start}")

Console results:

"C:\Program Files\Python39\python.exe" "D:/Google Drive/CompSci/proj/Alien Invasion/test.py"
pygame 2.0.1 (SDL 2.0.14, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Runtime: 40.15961766242981

Where normally the runtime should be almost instant...

I have exclusions in Windows Defender for all related folders: Windows Defender exlusions


Solution

  • According to this GitHub issue, a troubleshooting step is to do all of the inits separately, like:

    pygame.display.init()
    pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffersize=512, 
                          allowedchanges=pygame.AUDIO_ALLOW_ANY_CHANGE)
    pygame.mixer.init()
    pygame.joystick.init()
    

    This will make it obvious which one is slowing things down. In OP's case it was joystick.init() casuing the slowness, which was solved by disconnecting and re-connecting the keyboard.