imputil

Fredrik Lundh fredrik at effbot.org
Tue Nov 7 10:44:12 EST 2000


Magnus Heino wrote:
> Does anyone have any docs or examples on using imputil.py distributed with
> python2.0?
>
> I want to be able to import a module not located on the filesystem, but in
> a database. And from what I understand, the imputil modules shoule be able
> to help me do this. But as it is right now, I dont understand a thing of
> what it is doing...

this might work:

1. subclass ImportManager, and override the get_code
   method.  something like this:

        class myImport(imputil.ImportManager):
            def __init__(self, database):
                # store pointer to my database object
                self.__database = database
                imputil.ImportManager.__init__(self)
            def get_code(self, parent, modname, fqname):
                # fqname is a fully qualified module name
                try:
                    print "look for", fqname, "in database!"
                    code = self.__database.get(fqname)
                except NotInDatabase:
                    return None # not found
                print "found it!"
                code = marshal.loads(code)
                return 0, code, {}

(see the get_code docstring for the full story)

2. create an instance of this object

        >>> importer = myImport(database)

3. call the "install" method to add it to the import chain:

        >>> importer.install()

4. start importing stuff:

        >>> import mymodule
        look for mymodule in database!
        found it!
        >>> import string
        look for string in database!
        >>>

</F>





More information about the Python-list mailing list