Import pygame
pygame.init()
BG = pygame.image.load('_pycache_/test_bg.jpg')
def DrawGameWin():
window.blit(BG,(0,0))
pygame.display.update()
DrawGameWin()

- 202,892
- 27
- 131
- 174
-
1The error message sounds like the file can't be found on the hard disk. Please check your current working directory and also the path of the file you want to open! – csabinho Oct 01 '19 at 01:53
-
They are both on my desktop in a folder. I'm unsure of what to do next. Where should I move my images I want to upload. – Oct 01 '19 at 02:09
-
[Here](https://stackoverflow.com/a/50098973/6552776) you can see how to check your current work directory. Please check that you're assuming it correctly. Alternatively you could also provide the full path for now, but this will destroy portability, so just do this as a last option. – csabinho Oct 01 '19 at 02:13
-
maybe im not understanding. I ran the program showed the path. I put both in the same folder on my desktop. isn't that the same path? Shouldn't that resolve the issue? – Oct 01 '19 at 02:26
-
The run path(in other words the current working directory) is important, not folder in which your file is located. – csabinho Oct 01 '19 at 02:29
-
See also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Apr 30 '23 at 08:36
1 Answers
The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. Alternatively, you can create an absolute file path.
The name and path of the file can be get by __file__
. The current working directory can be get by os.getcwd()
and can be changed by os.chdir(path)
:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
An alternative solution is to find the absolute path.
If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()
) the relative filepath. e.g.:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
# [...]
# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')
surface = pygame.image.load(filePath)
The same can be achieved with the pathlib
module.
Change the working directory
import os, pathlib
os.chdir(pathlib.Path(__file__).resolve().parent)
or create an absolute filepath:
import pathlib
# [...]
filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)

- 202,892
- 27
- 131
- 174
-
It's not incorrect that `os.getcwd()` returns the current working directory; but you don't need to know the current working directory to resolve a relative file name, as that's exactly what the OS does for you with a relative file name. – tripleee Apr 30 '23 at 08:37
-
@tripleee No, you don't need to know that. But you can use this information to debug. And all the neybeeys who don't believe that and keep claiming that this this answer is wrong can see their mistake. – Rabbid76 Apr 30 '23 at 08:43