3

I am learning python using this website

I am trying to load an image like so:

background_image = pygame.image.load("background.jpg").convert()

I have found several other posts asking the same question, and all the answers are telling the poster to make sure the image is in the same folder as the .py file. Well, it is. I double checked.

I also tried putting it into a subfolder called 'images' and using this.

background_image = pygame.image.load(os.path.join("images","background.jpg")).convert()

In both cases I get the following error message.

pygame.error: Couldn't open saturn_family1.jpg

or

pygame.error: Couldn't open images\saturn_family1.jpg

When using the absolute path, it works. But shouldn't I be able to use a relative path like this?

Also, this happens when using the script package to execute in Atom. But when executing the actual python file, it works.

I am very frustrated, this doesn't seem to make any sense! What's going on?

The full error message is:

C:\Users\Manu\Dropbox\Python\ProgramArcadeGames
Traceback (most recent call last):
  File "C:\Users\Manu\Dropbox\Python\ProgramArcadeGames\Ch11\11_graphicsAndSound.py", line 26, in <module>
    background_image = pygame.image.load(os.path.join("images","saturn_family1.jpg")).convert()
pygame.error: Couldn't open images\saturn_family1.jpg
[Finished in 0.815s]

This is the .py directory: The <code>.py</code> directory

The <code>.py</code> directory

UPDATE: I ran the exact same code in sublime and it worked fine. I am assuming this is a problem with Atom, so I will just use Sublime from now on. Thanks to everyone who tried to help!

William Miller
  • 9,839
  • 3
  • 25
  • 46
Manu
  • 319
  • 1
  • 3
  • 11

3 Answers3

5

Putting the file in the same directory as your python program will work provided that is the current working directory. Since we know this is not the case, either you are somehow changing the working directory while the script is executing, or you are starting in some other directory. You could do this on the command line with

python ../myscript.py

but I don't suppose you're doing this. You say something about running the script under Atom, which may explain it. I don't have any experience with this, so I can't say offhand. Please put

print os.getcwd()

as the very first line in your program. Then you'll know where you're starting and whether the current directory is changing. If it's not changing, try running the script under Atom and from the command line to see if you get different results. We won't be able to solve the problem until we know exactly what is happening.

saulspatz
  • 5,011
  • 5
  • 36
  • 47
  • 1
    I made a brand new python file in this directory: `C:\Users\Manu\Dropbox\Python\ProgramArcadeGames\Ch11\test.py` The file contains only two lines: `import os` and `print(os.getcwd())` If I run the script in Atom, I get `C:\Users\Manu\Dropbox\Python\ProgramArcadeGames` If I run the exact same script in Sublime Text 2, I get `C:\Users\Manu\Dropbox\Python\ProgramArcadeGames\Ch11` which is the correct path. What is going on here?? – Manu Sep 21 '15 at 14:27
  • 1
    Weird. I tried this out on atom and I can't figure out what it's doing. I wrote a two-line script like yours, ran it atom, and it worked. Then I opened a file in another folder, then ran the original script, and it printed the name of the second folder. That makes sense, but I can't get it to switch back to the first folder, except by shutting down atom and restarting it. – saulspatz Sep 21 '15 at 16:01
0

Just use the absolute path for the picture. For example:

self.image = pygame.image.load('C:/Users/denis/Documents/python_practice/git_practice/Python_lessons/alien_invasion/images/ship.bmp')
Perry Coxer
  • 11
  • 1
  • 1
-1

Same error happened when I was coding with VS. I fixed that by changing the path of the image (use an absolute path here) .

Change pygame.image.load('/images/xxx') to

pygame.image.load('/Users/xxx/..../images/xxxx')

I am using the mac but it should be the same on other OS.

MingYi Ma
  • 1
  • 1
  • 2
    You should never use absolute paths for resources in programs, it carries the very very high probability of breaking when anyone else with a slightly different directory tree structure comes to try to run it. Always use relative paths if possible. – Reece Sep 06 '19 at 20:10