I am working on a mac and I am writing a game in python and pygame that needs some sound files to be present in the same directory where the source code is. The thing is it works when I run the source code through the python interpreter. It doesn't work when I try to run the executable that has been compiled by cxFreeze. I have searched the internet for solutions but found nothing.
When python is interpreted the cwd would be the directory where the source code is in. When it is compiled with cxFreeze and ran through the terminal, the cwd would change to my home directory. This messes things up because I want to make this game portable and I need the sound files to be in the same folder as the executable.
import os
# Load the sound files.
CWDPATH = os.getcwd()
BEEP1 = pygame.mixer.Sound(os.path.join(CWDPATH, 'beep1.wav'))
BEEP2 = pygame.mixer.Sound(os.path.join(CWDPATH, 'beep2.wav'))
BEEP3 = pygame.mixer.Sound(os.path.join(CWDPATH, 'beep3.wav'))
BEEP4 = pygame.mixer.Sound(os.path.join(CWDPATH, 'beep4.wav'))
This part of code is what's causing the problem. It runs smoothly when interpreted, but doesn't work when compiled with cxFreeze because os.getcwd() evaluates to the home directory. I have tried putting 'os' in 'packages' option in the cxFreeze script as well. It doesn't work and I have worked on this hours without a solution. How can I get this to work?
You can use
sys.argv
. The first element of the command line arguments is always the program itself: