So i am on raspberry pi 2 and I am trying to make a game and i have the .py file in the same folder as the png but its still not working. it gives me this error: Also im using pycharm

/usr/bin/python2.7 /home/pi/Documents/QFTPC.py
Traceback (most recent call last):
  File "/home/pi/Documents/QFTPC.py", line 8, in <module>
    wafflestage1 = pygame.image.load("avi.png")
pygame.error: Couldn't open avi.png

Process finished with exit code 1


import pygame
import random
gameDisplay = pygame.display.set_mode((1000,700))
pygame.display.set_caption("Waffle time")
black = (0,0,0)
white = (255,255,255)
wafflestage1 = pygame.image.load("avi.png")
def message_to_screen(msg,color,x,y,fs):
    font = pygame.font.SysFont(None, fs)
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [x,y])
def drawrect(color,x,y,w,h):
    pygame.draw.rect(gameDisplay, color, (x,y,w,h))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
  • try using the full path to the image. Mar 12 '15 at 22:54
  • what full math @SteveRobillard
    – user27834
    Mar 12 '15 at 23:01
  • 1
    something like /home/pi/images/avi.png Mar 12 '15 at 23:03

The file needs to be in the "working directory", that is the directory where the code is ran. That is not necessarily where the code is, especially as you're using and IDE (pycharm).

The solution is either:

1) use a full path for the file. Not recommended as your code has a hardcoded dependency on your file system.

2) add the directory where the file is as resources so that pycharm can add it to its PYTHON_PATH variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.