Module Help

Neil Hodgson nhodgson at bigpond.net.au
Fri Jun 28 06:50:48 EDT 2002


Juan M. Casillas:

> I want to create an empty module from C, and then add
> functions and definitions stored in another file, and
> some code that I will create on-the-fly, from C.

   Here is some Python code by Michael Hudson to create a new module and add
some code to it:

newm = new.module(name)
sys.modules[name] = newm
newm.__file__ = filepath
exec compile(open(filepath).read(), filepath, "exec") in \
        newm.__dict__

   You can change the open(filepath).read() to provide the code text in ay
way you want. I think you can use empty strings for the filepath variable.
Now you just have to work out how to call this from C which IIRC is well
covered in the embedding part of the "Extending and Embedding the Python
Interpreter" manual.

   Neil





More information about the Python-list mailing list