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

Tom Cumming tcumming at
Wed Oct 7 22:45:59 CEST 2009

Thanks!, but I already thought of your suggestion. I've already gotten 
the clear impression
that the amount of work to implement this is more than the ROI.

Having said that, it might work if on exiting the app could re-zip itself.

One _big_ problem with zip files or compiled python executables is 
moving the file
from machine to machine. It wouldn't be platform independent anymore.

What I like best so far, is to have something like:

#!/usr/bin/env python

""" Here lies python, "boot" code that connects to the sqlite db that pulls
from a known sqlite table a blob that is the actual application and 
executes it. The
boot code size must always be less than some size."""

Convince the python parser not to go here...

fillter bytes to the sqlite database offset.....

sqlite database data

End of File.

Now the trick is to get the sqlite library to start looking for the 
sqlite database
at, "offset".

Roger Binns wrote:
> tcumming123 at 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 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
> <<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