The "pygame.error: video system not initialized" most commonly occurs when you
forget to call
pygame.init() to initialize all imported PyGame modules or call
To solve the error make sure your PyGame application is initialized and uninitialized correctly.
The first thing you should verify is that you've called the
method at the top of your file.
Here is an example of a basic setup of a PyGame application from the docs.
import pygame # 👇️ call pygame.init() here pygame.init() screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() running = True while running: # pygame.QUIT event runs when the user clicks X to close the window for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # fill the screen with a color screen.fill("purple") # YOUR GAME HERE # flip() the display to put your work on screen pygame.display.flip() clock.tick(60) # 👇️ uninitialize all pygame modules pygame.quit()
Notice that we called the
pygame.init() method at the top.
pygame.init() screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() running = True
The method initializes all imported PyGame modules.
You can also initialize modules manually, however, calling
more convenient as it initializes all of your imported PyGame modules
pygame.init() multiple times won't cause any issues as subsequent
calls have no effect.
pygame.init(), the error is raised because the modules have not yet been initialized.
Therefore, make sure to call
pygame.init() at the top as shown in the code
Make sure to also call the
pygame.display.set_mode() method after calling
pygame.init() screen = pygame.display.set_mode((1280, 720))
pygame.display.set_mode() method creates a display surface of the
pygame.quit()at the end, not inside a loop
Another common cause of the error is calling
pygame.quit() before your code is
done running, e.g. in a
pygame.quit() method uninitializes all PyGame modules that have
previously been initialized.
In other words,
pygame.quit() reverses your call to the
When the Python interpreter shuts down, the
pygame.quit() method is called
automatically, however, you can also call the method in your code to terminate
the PyGame resources and continue.
pygame.quit()method is not called multiple times (e.g. in a
while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill("purple") pygame.display.flip() clock.tick(60) # ✅ call pygame.quit() here pygame.quit()
Notice that the method should be called after the application loop.
You can also try to call the
exit() function after calling
the error persists.
while running: # ... pass pygame.quit() exit()
You can also call the exit method on the
import sys while running: # ... pass pygame.quit() sys.exit()
pygame.quit() method uninitializes all PyGame modules but it doesn't exit
sys.exit() method is used to exit the program.
If you use PyGame without opening a visible display (e.g. for testing), try
SDL_VIDEODRIVER environment variable.
import os # 👇️ Set SDL_VIDEODRIVER environment variable os.environ['SDL_VIDEODRIVER'] = 'dummy' import pygame pygame.init() pygame.display.set_mode((1,1)) while 1: events = pygame.event.get() for e in events: pass
SDL_VIDEODRIVER environment variable to
dummy makes it so the
dummy video driver is used.
You can learn more about the related topics by checking out the following tutorials: