yougetagoldstar yougetagoldstar - 4 years ago 215
Python Question

atom: atom-runner cant find images / use them with pygame

using python 36,
pygame 1.9.3,
atom 1.13 text editor

First time asking a question here on stack overflow.

For some reason when loading up an image (in the same folder as the .py file) like so:

background_image = pygame.image.load('my background.png').convert()


set my blit line, and running it with atom runner I get this error:

pygame.error: Couldn't open my background.png


but when I double click on the actual .py file, my image loads fine.

also, if I actually type the full directory path of the image in atom, the image loads fine.

I'm pretty new to coding. I'm guessing this has something to do with "path"? I've only messed with paths to make atom runner use the proper pygame version, but I have no idea why it won't load my image file like the python IDLE does.

Answer Source

Your problem is that atom-runner uses the Atom project path as the working directory when running a program. Your program is assuming the working directory is same as the folder the image is in. You need to either set up an Atom project with that as the root folder, or use 'os.path' to set the proper path to the image, like so:

from os import path

working_dir = path.dirname(__file__)
background_image = pygame.image.load(path.join(working_dir, 'my background.png')).convert()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download