Skip to main content
GameDev.net gamedev.net
🔒 Locked

relative path for image.load()

Started by shakalandro Dec 28, 2008 at 3:37 PM 6 replies 8.8k views
Original Post
shakalandro
shakalandro
So I'm creating a pygame and I cannot seem to get the relative path on my pygame.image.load(os.path.join('', 'ship.jpg') to work, I get a cannot load error every time. I have all my files in the same folder on my desktop. I am running mac os 10.5.6.
Hollower
Hollower

Paths can be tricky in Python. There are many different ways to launch a py file making the "current working directory" different in each case. You can use os.getcwd() to see what your cwd really is.

So most people recommend using absolute paths. Here is the accepted cross-platform way to get the absolute path of the py file that is currently being executed. I know it looks a bit convoluted but once you get the correct value just store it like I do here in the mypath variable:
mypath = os.path.dirname( os.path.realpath( __file__ ) )pygame.image.load( os.path.join(mypath, 'ship.jpg') )


...from here just use mypath (don't need to recalculate it every time).
I hope this works for you.

doctorsixstring
doctorsixstring
The simplest solution is to just store your art in the same directory as your py script, and use a relative path to load it.
swiftcoder
swiftcoder
Quote:
Original post by doctorsixstring
The simplest solution is to just store your art in the same directory as your py script, and use a relative path to load it.
Right, but this only works if your script is run from the same directory it resides in. While this tends to be the case on Windows or Linux, on a Mac it is only the case if you explicitly run the script from the command line (double clicking the script from the Finder will give you odd working directories).
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
doctorsixstring
doctorsixstring
Wow, you learn something every day. Hollower's comments make more sense now. I'll keep that in mind, since I want to support Mac with my Python game development, which has so far been limited to Windows and Linux.
shakalandro
shakalandro
Alright, I have several files to load though. So if I do

image.load(os.path.join(os.path.realpath(filename)), "filename")

it should be relative no matter where I distribute my file. This game is for a class. I need to upload the file to my class website, so this why the paths are crucial.
shakalandro
shakalandro
Awesome, it worked, thank you guys, that should do it.
swiftcoder
swiftcoder
Quote:
Original post by doctorsixstring
Wow, you learn something every day. Hollower's comments make more sense now. I'll keep that in mind, since I want to support Mac with my Python game development, which has so far been limited to Windows and Linux.
Pyglet recently added a resource module, which handles figuring out file names across platforms and packaging systems (py2app, py2exe, etc.). Even if you aren't using Pyglet, worth a look to see how it is dealt with.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.