pygame.error: Couldn't open image

孤人 提交于 2019-12-01 00:46:31

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.

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.

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')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!