How to statically bind modules written in Python

jay.krell at cornell.edu jay.krell at cornell.edu
Thu Oct 19 02:53:08 EDT 2000


Solution 3b: put the files' contents in strings. Modify wherever it is in Python that resolves "import" to know about your built in ones. It should not be hard.

You could even write a few lines of Python to turn the .py file
-- foo.py --
abc
123
this is supposed to be Python code
-- foo.py --

into a C source file

const char foo_py[] =
"abc\n"
"124\n"
"this is supposed to be Python code\n"
;

Then you'll need a table somewhere
const static struct PyMap { const char* name, const char* contents; } pyMap[] = { { "foo", foo_py } };

you could even put the string right in the array, depending on how/if you automate the build.

This is a pretty standard trick. You can embed binary files in source similarly. Even write out .dlls at runtime for like a runtime install. You don't need "resources" for any of this, and the data approach works better if you want to put anything in static .libs..

 - Jay

    -----Original Message-----
    From: Travis Nixon <tnixon at avalanchesoftware.com>
    To: python-list at python.org <python-list at python.org>
    Date: Wednesday, October 18, 2000 1:12 PM
    Subject: How to statically bind modules written in Python
    
    
    Ok, after quite a lot of trouble, I've got a Windows version compiling that doesn't require the python dll in any way shape or form.
    
    I've also figured out how to statically add C based modules, so they can be imported on a system that doesn't contain anything other than the statically linked python.exe.  Obviously, I won't be able to import outside modules from dlls or .py files, but I'm ok with that.
    
    Now, where I'm running into a problem is that there are going to be some .py files that I need to be able to import, but I can't for the life of me figure out how to bind them statically, so they're available for scripts run by my statically linked python.exe.  Eventually, this is going to be the base for using python embedded into a project that doesn't have (as far as I know) support for any type of dynamic linking.
    
    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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20001018/7152fa03/attachment.html>


More information about the Python-list mailing list