How to statically bind modules written in Python

Alex Martelli aleaxit at yahoo.com
Thu Oct 19 06:24:57 EDT 2000


"Travis Nixon" <tnixon at avalanchesoftware.com> wrote in message
news:mailman.971899655.18689.python-list at python.org...
    [snip]
Solution 3: I just thought of this, but it probably would be possible to
include the .py files (or rather the .pyc files) in the resources, and
rewrite the python's import to grab them from there instead of going to
disk.  The major problem here is that our final platform is not going to be
windows.  I suppose I could do something similar though, with a file that's
loaded into memory at startup, and then do the same sort of modifications to
import to search there instead.  This is actually a fairly workable option,
but it's still just not as clean and elegant as simply being able to somehow
statically bind the .py module to the executable itself.  (which as I'm
writing this I'm realizing is probably impossible anyway)


I think you can simulate Windows resources in a portable way (and import
hooks will let your Python import statements look to those simulated
resources instead of looking to the filesystem).  A simple utility can
take any "resource" file you would like (e.g. "foo.pyc" -- using the
.pyc seems better than using the .py, clearly) and turn it into a .c
source file that stuffs those bytes into an array of bytes, and sets
its name as a key into some simple global structure.  Compiling and
linking those generated C sources with the rest of your application
should let you be on your way.  See http://www.python.org/sigs/import-sig/
for more (also builtin module imp, and example module knee, in the
Python 2 standard distribution).


Alex







More information about the Python-list mailing list