Resource Not Found Exception in pyglet

Viewed 5784

I'm using Python 2.6.6 and pyglet 1.1.4. In my "Erosion" folder, I have "Erosion.py" and a folder named "Images." Inside images, there are .png images. One image is named "Guard.png."

In "Erosion.py" there is a segment that goes like so:

pyglet.resource.path = ['Images']
pyglet.resource.reindex()
self.image = pyglet.resource.image('%s%s' % (character, '.png'))

When I run this, I am given

File "C:\Python26\lib\site-packages\pyglet\resource.py", line 394, in file raise ResourceNotFoundException(name)
ResourceNotFoundException: Resource "Guard.png" was not found on the path.  Ensure that the filename has the correct captialisation.

I have tried changing the path to ['./Images'] and ['../Images']. I've also tried removing the path and the reindex call and putting Erosion.py and Guard.png in the same folder.

6 Answers

I get a problem like this using pyglet and pyscripter.(the text editor) In order for the file to be found I have to restart the editor before running the program.

This might be a problem with pyscripter however.

It is worth looking into: http://pyglet.readthedocs.io/en/pyglet-1.3-maintenance/programming_guide/resources.html

You should include all the directories into:

 pyglet.resource.path = [...]

Then pass the ONLY the file name when you call:

pyglet.resource.image("ball_color.jpeg")

Try:

import os
import pyglet

working_dir = os.path.dirname(os.path.realpath(__file__))
image_dir = os.path.join(working_dir, 'images')
sound_dir = os.path.join(working_dir, 'sound')

print working_dir
print "Images are in: " + image_dir
print "Sounds are in: " + sound_dir

pyglet.resource.path = [working_dir, image_dir, sound_dir]
pyglet.resource.reindex()

try:
    window = pyglet.window.Window()
    image = pyglet.resource.image("ball_color.jpeg")

    @window.event
    def on_draw():
        window.clear()
        image.blit(0, 0)

    pyglet.app.run()

finally:
    window.close()

The use of try and finally are not necessary, but they are recommended. You might end up with a lot of open windows and pyglet back processes if you don't close the window when there are errors.

pyglet 1.4.4 format: HUD_img = pyglet.resource.image('ui\ui_gray_block_filled.png')

pyglet 1.4.8 format: HUD_img = pyglet.resource.image('ui/ui_gray_block_filled.png')

I was using pyglet 1.4.4 and was able to use this the first format. After upgrading to pyglet 1.4.8 I had the error: pyglet.resource.ResourceNotFoundException: Resource "your_resource_path" was not found on the path.

Switching to the 2nd format with forward slashes solved this issue for me. Not sure why this change was made...I'm sure there was a good reason for it.

Related