How to include a directory of files when makine onefile

22 views
Skip to first unread message

code scribbler

unread,
Jul 4, 2020, 1:48:15 PM7/4/20
to PyInstaller
Hi,

I am completely new to using this and need some help.

I am a new python programmer. I made a game that uses some images in a folder named images. The game also uses some sounds in a folder named sounds.
I can't seem to figure out how to include these in the pyinstaller command to generate an .exe file.

I have my game.py, images, and sounds directories all in the same location.

I have looked at a lot of different tutorials that all only show you how to use pyinstaller with just a single .py file

(for some reason everything after this is in itallics and this site wont let me change it?)
 I have tried to use the option:

--add-data <SRC;DEST or SRC:DEST>


But I have no idea what SRC;DEST is supposed to be. Some of the tutorials say to just put in your folder with a slash at the end
like pyinstaller --add-data "img/"  but then I would need the ; and a DEST right? what is that?

Can someone please tell me the command to type to make this work.  It is just my game.py and a img and snd directory. Like exactly what to type to make this work. Please.

I have tried many variations of the command like:
pyinstaller --add-data "img/";"img" game.py
but nothing seems to work. I always get an error for the background image file not being found. (it is the first file from these directories that my code accesses.)

I have also tried using cxfreeze with the exact same problem. No idea how to add directories. It is mentioned in a lot of youtube tutorials but never is complete.

Thank you for any help you can provide.

bwoodsend

unread,
Jul 6, 2020, 3:38:57 AM7/6/20
to PyInstaller

Hi,

Firstly I suggest to use --onedir (the default) instead of --onefile to debug then switch back to --onefile once it’s working. --onefile does everything --onedir does but hidden in a temporary directory so you can’t see anything that’s happening. With --onedir mode, you’ll be able to navigate into the dist folder and check if/where your files have been included.

I’m assuming your project structure is as follows. Correct me if it’s different:

+-- game.py
+-- images
|    +-- image1.png
|    +-- image2.jpg
+-- sounds
|    +-- noise1.wav

And your shell/cmd prompt is in running in the same folder as game.py.

Secondly, as you’re a beginner I want to be sure you know how to use external files portably in regular non-PyInstaller Python without making any assumptions about the current working directory. Something like this is the usual method. Test yours by cd ing to a different folder, then running python /full/path/to/game.py. If it works then you’re ready to start using PyInstaller.

Your guess pyinstaller --add-data "img/";"img" game.py is the correct syntax and it should work. The DEST is where inside your application it to go relative to the app's root (this will make more sense once you’ve used the --onedir option). The quotes are unneeded unless you have names with spaces and you have to use : instead of ; on macOS.

Let us know if you're still getting stuck,

Brénainn

Reply all
Reply to author
Forward
0 new messages