forcing import without execution?

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Dec 3 05:52:43 EST 2001


"Peoter Veliki" <peoter_veliki at hotmail.com> writes:

> can you import a module (make the namespace available) without actually
> executing the code?

Depends on what you mean "make the namespace available". If you want
that a module with that name is available in sys.modules, and
available in the local namespace, you can create one using the new
module:

>>> import new,sys
>>> sys.modules['foo']=new.module("foo") 
>>> import foo

If you want the module also to contain all the definition from the
source code, you must execute it - classes and functions come into
life only by being executed.

I would recommend that you do not use the same module for exec'ing and
importing. Instead, create a module foo that contains just the classes
and functions, and one source file bar that is available for exec'ing;
bar should start with

from foo import *

HTH,
Martin




More information about the Python-list mailing list