-1

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!

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 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. –  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,967
  • 31
  • 40
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
  • 869
  • 2
  • 19
  • 26