0

SOLVED: Sorry for the useless post. It turns out my flashy new IDE was all screwed up, and wasn't saving .py files. (I'm using atom). I noticed this and took it back to the old school IDE, which worked just great when I put it into the file I wanted.

I am making a simple game with pygame, and I want it to eventually be shareable with others. However, my code manually changes the directory of the python file each time, which uses a path. I thought just putting the code in a folder in file explorer would work to change the directory, but it did not, so I used os.chdir(r'C:\Users\lukep\Downloads\Gladiator'). This works great on my computer, but it won't on anyone else's. Is there a way to make sure my program is in the same directory as the images I need without making a specific file path? Any help is welcome. Thanks!

  • make the pyhton script take the path as an argument. That way the path can be passed when ur running it – leoOrion Mar 24 '21 at 10:57
  • You can also look at this - https://stackoverflow.com/a/4028943/4983469. – leoOrion Mar 24 '21 at 10:58
  • I use relative paths, i.e., relative to where my main script is. E.g., if my script is in C:/foo/bar then I'll have my images in C:/foo/bar/images and I'll reference these in my script as images/exit.png. If need be, I'll do os.chdir(os.path.dirname(__file__)) in my script. – Justin Ezequiel Mar 24 '21 at 11:02

2 Answers2

0

Hard to give a definitive answer without seeing your full code, however as a rule of thumb I would say try to make use of relative paths in your code.

Also, this SO question shows how to determine which directory your script is running in:

How do you properly determine the current script directory?

os.path.dirname(os.path.abspath(__file__))
Marcello Romani
  • 2,393
  • 22
  • 34
0

Try making a new system path variable with the respective directory address 'C:\Users\lukep\Downloads\Gladiator'. Go to System propertiesAdvanced system settingsEnvironment variablePath and add new path variable by pasting 'C:\Users\lukep\Downloads\Gladiator'.

andselisk
  • 610
  • 2
  • 14
  • 23