0

I recently began work on a game for my daughter to help with learning words. I am moving along and decided to create the executable file (I'll done this with many other games). This game is somewhat different due to sounds and music. I have tried everything I can think of, searched everything I can think of etc. This is the error CMD reports, the error is referring to a sound file. I have tried adding the file directly with --add-data I have tried placing the executable in the same directory as the sound file (which shouldn't be needed since it should bundle it already). The script runs absolutely fine otherwise (from CMD etc.) ANY IDEAS??

C:\Users\launc\Desktop\Coding\Games\g_sight_words\dist>sight_words.exe pygame 1.9.4 Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "sight_words.py", line 5, in <module> File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "c:\users\launc\appdata\local\programs\python\python37-32\lib\site- packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module exec(bytecode, module.__dict__) File "settings.py", line 21, in <module> pygame.error: Unable to open file 'the.wav' [9892] Failed to execute script sight_words

Vega
  • 27,856
  • 27
  • 95
  • 103
  • 1
    Please read how to provide [mcve] and why it's important – Vega Feb 05 '19 at 10:13
  • And don't use quote blocks to quote output, I know that sounds logical. But use code blocks instead to preserve indentations and other things. As of now, that error message is barely readable to anyone. – Torxed Feb 05 '19 at 11:35
  • 1
    Secondly, It would be appreciated if you showed us how you actually bundle the executable. Are you using the `-F` flag to bundle it as one executable, how are you passing in `--add-data`? What does your setup script look like? [tihs](https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file) might help you. – Torxed Feb 05 '19 at 11:40

1 Answers1

0

What does your .spec file look like? Here's the PyInstaller docs on adding data files.

Basically you need to add something like:

a = Analysis(...
 datas=[ ('the.wav', '.') ],
 ...
 )

This will put your sound file ('the.wav') into the root directory of your compiled application (the second argument, '.')

Then in your application you can check if you're running from source or as a compiled executable. I use a helper function:

def my_path(path_name):
    """Return the appropriate path for data files based on execution context"""
    if getattr( sys, 'frozen', False ):
        # running in a bundle
        return(os.path.join(sys._MEIPASS, path_name))
    else:
        # running live
        return path_name

So then your application code will look something like:

the_sound = pygame.mixer.Sound(my_path("the.wav"))
import random
  • 3,054
  • 1
  • 17
  • 22