无法在 Atom 中使用 PyGame 打开图像 [英] Cannot Open Images With PyGame in Atom

查看:21
本文介绍了无法在 Atom 中使用 PyGame 打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

更新:我在 sublime 中运行了完全相同的代码,并且运行良好.我假设这是 Atom 的问题,所以从现在开始我将只使用 Sublime.感谢所有试图提供帮助的人!

解决方案

将文件放在与 Python 程序相同的目录中即可,前提是该目录是当前工作目录.由于我们知道情况并非如此,因此您要么在脚本执行时以某种方式更改了工作目录,要么在其他目录中启动.您可以使用

在命令行上执行此操作

python ../myscript.py

但我不认为你在这样做.你说的关于在Atom下运行脚本的事情,可以解释一下.我没有这方面的经验,所以我不能随便说.请把

打印 os.getcwd()

作为程序中的第一行.然后你就会知道你从哪里开始以及当前目录是否正在改变.如果它没有改变,请尝试在 Atom 下和从命令行运行脚本,看看是否得到不同的结果.在我们确切地知道发生了什么之前,我们将无法解决问题.

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 imagessaturn_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:UsersManuDropboxPythonProgramArcadeGames
Traceback (most recent call last):
  File "C:UsersManuDropboxPythonProgramArcadeGamesCh1111_graphicsAndSound.py", line 26, in <module>
    background_image = pygame.image.load(os.path.join("images","saturn_family1.jpg")).convert()
pygame.error: Couldn't open imagessaturn_family1.jpg
[Finished in 0.815s]

This is the .py 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!

解决方案

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.

这篇关于无法在 Atom 中使用 PyGame 打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆