link vs import

Alex Martelli aleaxit at yahoo.com
Sat May 26 03:00:05 EDT 2001


"Brian Rowe" <roweb004 at hawaii.rr.com> wrote in message
news:n7BP6.6673$WI.2037477 at typhoon.hawaii.rr.com...
> In article <9elnm40131d at enews2.newsguy.com>, "Alex Martelli"
> <aleaxit at yahoo.com> wrote:
>
> > PyImport_AppendInittab will let you add your module to the table of
> > "built-in" modules.  PyImport_ImportModule lets you "load" it.  You may
> > then place an entry for it in module '__builtin__' or wherever you wish.
> >  Is that what you mean...?
>
> So if I link it in at compile time and do a PyImport_AppendInittab on it,

...then you place it among the 'builtin modules' of this Python
interpreter.  The module's initialization function is not called
until the module IS loaded, just as for other mmodules.

> and then later in my scripts that need it call PyImport_ImportModule I'll

PyImport_etcetc are functions in the Python-C API library, to be called
by C code that's extending or embedding Python, not by scripts.  A
script might just "import themodule" instead.

The idea of calling PyImport_ImportModule would be to SUBSTITUTE
for the import statement (or other way to importing from scripts), e.g.
by placing the module object thus loaded in the __builtins__ module.

> then have access to it from the scripts?

A script will access a module if it imports it (best because most
explicit, in most cases) OR the module is placed in the builtins.


Alex






More information about the Python-list mailing list