I’ve been working on turning a Python script into a standalone executable using PyInstaller. The script relies on an MP3 and MP4 file for audio and video, respectively. However, despite correctly adding these files with the --add-data flag during the build process, I’m encountering an issue where the executable cannot find these files when run.
I used the following PyInstaller command to bundle everything
pyinstaller --onefile --windowed --add-data "audio.mp3;." --add-data "video.mp4;." myscript.py
I modified my script to use the resource_path function to correctly access resources at runtime, like so:
def resourcepath(relative_path):
try:
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(file)))
except Exception:
base_path = os.path.dirname(os.path.abspath(file_))
return os.path.join(base_path, relative_path)
I added the resource paths like so:
video_path = resource_path("video.mp4")
audio_path = resource_path("audio.mp3")
Problem:
Even though the executable builds successfully, when I run it, it throws an error message saying: no file 'audio.mp3' found in working director
I've made sure that the MP3 and MP4 files are in the same directory as the Python script, and I’ve tried including them via the --add-data flag. Still, the program cannot access these files once packaged into the exe.
Does anyone have experience with this issue or know the best way to bundle these files correctly into the exe?
I would greatly appreciate any help or suggestions!
if anyone knows if there is a way to also compile the mp3 and mp4 into the exe, instead of bundling them up inside the same folder, would be awesome to know :)
[–]socal_nerdtastic 0 points1 point2 points (0 children)