Re: .py source in interpreter exectuable?

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Fri, 17 Jun 1994 22:29:30 -0400

On Jun 17, 19:34, Dan Connolly wrote:
> Subject: .py source in interpreter exectuable?
>
> If I were shipping a product consisting of a python interpreter, some
> custom C modules, and a bunch of .py source modules, I'd like to be
> able to bind them all in one big happy executable to save my users all
> the installation hassles involved with $PYTHONPATH and such.
>
> I've done this with X resources files... write a little perl ditty
> that converts .res files to a c array. Compile the array, and then at
> runtime, iterate over the array and call XrmPutLineResource().
> Presto! app-defaults without the $XUSERFILESEARCHPATH hassle.
>
> I suppose one could use the same trick with python... just put
> the .py in the executable as string constants and use ceval()
> or whatever to introduce the modules into the interpreter.
>
> Anybody done this?
>

Or better yet, save the compiled .pyc byte-code string.
Printed from python, it should need little editing to
turn into a C string literal - they both use the same
\000 escape convention.

The .pyc file contains the marshaled byte code for a
single code object, which when executed will create
all of the objects and bindings defined in the module.

You could create a module in C that defined the byte-code
string as a string literal, and a method than did the
internal functional equivalent of an "import" : exec
the string and return a module with a namespace dict.

And if you were going to do this regularly, it wouldn't
be too difficult to make a script that automagically
generated the c module code to do this.

( Or is there already an easier way? )

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics