Python error pygame error Couldn t open pygame png

+1 vote

I'm trying to build the snake game using pygame. I'm following this tutorial.

I end up with the following error:

pygame.error: Couldn't open pygame.png

Jun 19, 2019 in Python by Jinu
8,162 views
yea I'm having a similar experience as well. I've tried different images. Please can someone be of any assistance
Hi @juno,

Can you please share your directory structure(where the code you are executing is saved)?

You usually get this error when the package i.e. pygame, in this case, is installed in a different directory.

1 answer to this question.

0 votes

icon = pygame.image.load('spaceship.png')
pygame.error: Couldn't open spaceship.png

I am getting this error,\anyone?

Hope this Helps!!

To learn more, join the online course to do Masters in Python.

Thanks!

answered Apr 9, 2020 by Ali

Hey, @ Ali,

It will be very helpful if you can post your code. Though I can suggest you something like better to use Relative paths instead.

current_path = os.path.dirname(__file__) # your .py file is located
current_path = os.path.dirname(__file__) # the resources folder path
image_path = os.path.join(resource_path, 'images') # the image folder path

By doing this, wherever you move the folder containing your .py file, its subdirectories (and therefore whatever they contain) can still be accessed without you having to modify your code.

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

player_image = pygame.image.load(os.path.join(image_path, 'spaceship.png'))

I hope this will be helpful.

Where should i type that in and how? ( Im sorry im a begginer)

Hi, @There,

You have to add in the file that has the script.

What does resource path mean?

Hi, @There,

Every application usually ships with various resources, such as image and data files, configuration files and so on. Accessing those files in the folder hierarchy or in a bundled format for various platforms can become a complete task, for which the resources module can provide ideal supportive application components.

The Resource class allows you to manage different application data in a certain directory, providing a dictionary-style access functionality for your in-application resources.

it is showing os no tdefined help me please

Hey, @Sumit,

Do you import os?

Just add:

import os

in the beginning, before:

from settings import PROJECT_ROOT

This will import the python's module os, which apparently is used later in the code of your module without being imported.

I hope this will be helpful.

So   the  place where my file should be...................should  it's  format be like  (for example)  :     ThisPc/Desktop/_.py..............................?
Hi, @There

Which format you are concerned about? I have spoken about importing OS.
I don't know...............I'm new

so  I  don't  know..........my code is this :

import pygame
import os
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((1100, 600))

current_path = os.path.dirname(MyGame.py)
resource_path = os.path.join(current_path, 'resources')
image_path = os.path.join(resource_path, 'images')

player_image = pygame.image.load(os.path.join(image_path, 'aim.png'))

running = True
while running:

    screen.fill((0, 0, 28))

    screen.blit(playerImg, (550,300))

    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

Hey, @There,

Are you facing the exact same error which is mentioned above while executing your code?

my code is like this

import pygame
import sys
screen = pygame.display.set_mode([680, 480])
pygame.init()
screen.fill([130, 0, 200])


class Block(pygame.sprite.Sprite):
    def __init__(self,file_name,location):
        img = pygame.image.load(file_name)
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width,height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
pygame.image.load('ball(2)')
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.flip()

Hi, @There,

So using this code you want to conclude that there is no error after executing it?

Related Questions In Python

0 votes
2 answers

Python error "pygame.error: Couldn't open pygame.png"

add import os at top of your ...READ MORE

answered Sep 25, 2020 in Python by anonymous
932 views
0 votes
1 answer

Python pygame error : Failed loading libpng.dylib: dlopen(libpng.dylib, 2): image not found

Try installing libpng You can do it with ...READ MORE

answered May 31, 2019 in Python by SDeb
• 13,300 points
972 views
0 votes
1 answer

Python error "ImportError: No module named pygame.locals"

You need to download and install the ...READ MORE

answered Jun 18, 2019 in Python by Greg
4,849 views
0 votes
1 answer

Python error "ValueError: size needs to be (int width, int height)" while using pygame.Surface

pygame.Surface takes in integer values for building ...READ MORE

answered Jun 19, 2019 in Python by Varsha
2,672 views
+2 votes
2 answers

Error while printing hello world in python.

You must be trying this command in ...READ MORE

answered Mar 31, 2018 in Python by GandalfDwhite
• 1,320 points
2,214 views
0 votes
1 answer

Create and open a file in Python

file = open('text.txt', 'w+) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
345 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,811 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
2,090 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP