Jump to content
  • Advertisement
Sign in to follow this  

relative path for image.load()

This topic is 3431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!