[mod_python] Re: vampire.importModule from module import function/class?

Graham Dumpleton grahamd at dscpl.com.au
Wed Apr 20 21:14:42 EDT 2005


On 21/04/2005, at 10:56 AM, Bram wrote:

> Are there any special options that can be given to 
> vampire.importModule() that duplicates "from module import 
> function/class" style imports or should I just stick to the long 
> dotted notations for calling these functions/classes? It sure would be 
> nice to only have to load the code for the functions/classes I needed.

I think you misunderstand what "from module import function/class" does.
It still loads the whole contents of the module into the process. It
doesn't selectively load just the bits you indicate. It is in effect a
short cut for saying:

   import mymodule
   myfunction = mymodule.myfunction
   myclass = mymodule.myclass

As an example, try the following:

   from base64 import decode
   print globals()
   import sys
   print dir(sys.modules["base64"])

So, "decode" may be the only thing in your namespace, but the whole 
module
still exists in sys.modules.

To avoid dotted notation, you can setup references in your local 
namespace
using:

   mymodule = vampire.importModule("mymodule",directory)
   myfunction = mymodule.myfunction
   myclass = mymodule.myclass

Note that the autoreloading mechanism of the Vampire import system 
actually
relies on the module being in the globals namespace if imported at that
level, as it uses that as a means of determining parent/child import
dependencies and thus when it needs to reimport a parent because of a
change in a child.

Thus, although you could say:

   mymodule = vampire.importModule("mymodule",directory)
   myfunction = mymodule.myfunction
   myclass = mymodule.myclass
   del mymodule  ### BAD

you would then cause the full abilities of the autoreloading mechanism
to not work properly.

BTW, Vampire doesn't put its loaded modules in sys.modules so if you go
looking there for them, you will not find them.

Graham





More information about the Mod_python mailing list