I have been looking for a way to compile my whole program into a single executable file that can be run completely by itself. That also includes any image files that my program uses. Something like when the executable is run, all extra files are extracted into a temporary directory relative to where the file is located at. After the script finishes or is closed, the temporary files are deleted.

I don't know if I'm just dreaming here, but so far I haven't found what I'm looking for. I've tried py2exe and pyinstaller, but as far as I can tell, they don't package everything into a single file.

If anybody has any help for me here, I appreciate it.

py2exe can package into a single file. You just have to tell it to. In your setup.py's setup function, adjust your options argument to include the following:

options = {"py2exe": {"compressed": 1,
                          "optimize": 2,
                          "ascii": 1,
                          "bundle_files": 1}}

I also noticed that I have zipfile=None as an argument to setup in my setup script. I don't exactly remember what it's for though (it may or may not be related).

I have a setup script that I got from somewhere, I can't remember where, that combines everything into a single file. The options are set the same as you list them there. But it doesn't combine other files that my program uses, such as jpg files or png files for a gui interface. I'm looking for a way to package these into a single file so I don't have to distribute them as well as the exe.

Give this a try to include image files and all into one exe file ...

"""
Py2ExeWinSetup.py

Py2Exe (version 6.6 and higher) setup file for windows/GUI programs.
Creates a single .exe file.

Simply add this file into the same folder with the source file.
Change your source filename at the bottom to whatever you called your 
code file.  Now run Py2ExeWinSetup.py ...

Two subfolders will be created called build and dist.
The build folder is for info only and can be deleted.
Distribute whatever is in the dist folder.

The dist folder contains your .exe file, any data files you specified
in "data_files =", MSVCR71.dll and w9xpopen.exe

Your .exe file contains your code as . pyo optimized byte code files, 
all needed modules and the Python interpreter (as a Pythonxx.dll).
MSVCR71.dll can be distributed, but is often already in the system32 
folder. 
w9xpopen.exe is needed for os.popen() only, can be deleted otherwise.
"""


from distutils.core import setup
import py2exe
import sys

sys.argv.append("py2exe")
sys.argv.append("-q")

setup(
  options = {"py2exe": {"compressed": 1,
                        "optimize": 2,
                        "ascii": 1,
                        "bundle_files": 1}},
  zipfile = None,
  # to add .dll or image files use list of filenames
  # these files are added to the dir containing the exe file
  data_files = ['apple.jpg', 'cheese.jpg'],

  # replace 'pyg_Image3.py' with your own code filename
  # (replace windows with console for a console program)
  windows = [{"script": 'pyg_Image3.py'}]
)

Yes, zipfile=None is important if you just want one exe file with everything in it! Haven't totally tested the code, so you are my labrat.

Editor's note: Corrected the data_file part. The images are not included in the executable file itself.

Comments
Nice !! :)
nice example code
thanks, I've been looking for something like this!
Ooh, i like! :)

Yes, zipfile=None is important if you just want one exe file with everything in it! Haven't totally tested the code, so you are my labrat.

Well i tried it and it worked fine. Probably the best setup script i have come across, thanks vega.

Yes, but doesn't this script only copy said image files to a new location relative to the executable file?

Yes you could do this, but I was only speaking of images as one example. What if perhaps my script needed extra text files or zipfiles?

Yes, but doesn't this script only copy said image files to a new location relative to the executable file?

I finally got around to test this with the audiovisual module pyglet. You are correct, the image files are included in the same directory with the executable file, but not in the file itself.

Unfortunately the py2exe info is a mess! There should be a way to include resource files.

Henri is on to the possible solution, since you could convert any binary data file to a base64 encoded string.

This article has been dead for over six months. Start a new discussion instead.