foresightyj
foresightyj

Reputation: 2106

How to embed a text file into a single executable using py2exe

I have a python script which reads a large dictionary file which is stored as a .txt file. Now I want to make a single executable using py2exe. Currently, my setup looks like this:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

Mydata_files = [('.', ['dict_lookup.txt'])]


setup(console=['BladeHost.pyw'],
  options = {
    "py2exe": {
        'bundle_files': 1, #with this == 1, I have the weird CreateActCtx error msg
        'compressed': True,
        "dll_excludes": ["MSVCP90.dll", "gdiplus.dll"],

        }
    },
  zipfile = None,
  data_files=Mydata_files,
  )

This setup gives me the executable and the dict_lookup.txt in the dist folder. However, I do not want this stupid .txt file outside of the executable. I know I copy and paste this text file into a long string inside the python script but that will make my python script ugly.

So my question is, is there a way to setup py2exe so that this .txt file is built into the executable?

Thanks for any help.

Upvotes: 1

Views: 2871

Answers (1)

Morwenn
Morwenn

Reputation: 22552

Putting it into a Python file as a string is actually the way things are most often done. If you have a look at the PyQt resource system for example, you will see that they embed any resource (images, sounds, etc...) as huge byte strings in some Python files that you should not modify afterwards.

Just make a separate Python file called resources.py or something like that if you want to keep your code clean. Then import the variables from that file.

Upvotes: 2

Related Questions