Combining python and sqlite DB into a single, "executeable".

Roger Binns rogerb at rogerbinns.com
Wed Oct 7 02:15:25 EDT 2009


tcumming123 at gmail.com wrote:
> The problem, is that I need the python app and the
> sqlite db file to exist in the same disk file. This way the app to
> access the data and the data are in the same file.

For binaries this is possible with a little hackery.  Firstly you need make
the app be a zip file.  The good news is that zip files store their
information at the end while executables store their information at the
beginning.  This is how self extracting zips work - they are the extractor
program followed by the zip data.

You can store the sqlite database in the zip portion.  Copy it out to a
tempfile while running and put it back in when done and saving.  The
tempfile module will help with temporary files.  The zipfile module works
quite happily with zip files prepended with extra stuff.

To get started make a zip file with dummy contents (for example a readme
explaining that mail is stored within).  Assuming your app is called
mail.exe and the zip file is called mail.zip you just concatenate them.
py2exe and several other programs can make an executable out of a Python
script for you.  However in some cases they use this same 'trick' of an
appended zip file to store the python code so you could also just modify that.

If your app is a python script and you want to distribute it that way then
you can make a zip file of the python script and then you have to run it by
setting the PYTHONPATH environment variable to point at the zip file.

Note that once you try this you may find the operating system and/or python
not being too happy about modifying what they are "executing".

If you are feeling really adventurous there may even be a way of following
the script with the data.  This is used in Unix shell scripting but the Unix
shell doesn't check the whole file is syntactically correct so you can
append any binary junk you want.  For Python you could try something like

---- 8< ----
#! python

python code
python code

""" SOMESPECIALMARKER
<<sqlite file contents>>
"""
---- 8< ----

You'd have to do some escaping of the contents or maybe just base64 encode it.

Roger




More information about the Python-list mailing list