py2exe, executable: How to pack data into single file?

833 views Asked by At

I am wondering of how to pack several files into one single windows executable file with py2exe. I could get all python files (dll's, ...) into one py2exe. But how can i copy some of my own files (i.e. images or configuration files) into my executable and load them into python with a relative path. In Example:

file = open("config.txt",r")
file2 = open("mysubfolder\config.txt","r")
2

There are 2 answers

5
noname On

I don't know if you can embed your image files into the exe. If there is a way, I'm also interested. What I know is that you can use them from a separate folder. For this, you need to modify your setup file. Add a data path for the thing you want to load. For example, add this to your setup file:

import glob

data_files = [('mysubfolder', glob.glob('mysubfolder/config.txt'))]

setup(
   data_files = data_files,
   windows = [GUI2Exe_Target_1]
)

Of course, you can add more folders or files to data_files. And for other configuration options in this setup file, you can refer to the py2exe official site.

1
Zenadix On

If you are using PyQT (or PySide), it's possible to compile resource files into python modules (.py files). That way, you just need to import them to be able to use them, and py2exe will include them in the executable by default.

Look at the PyQt4 Resource System.