dynamic import/reload of modules

Quinn Dunkan quinn at retch.ugcs.caltech.edu
Tue Aug 7 21:08:30 CEST 2001


On Tue, 07 Aug 2001 10:54:34 GMT, Thomas Weholt <thomas at gatsoft.no> wrote:
>Say I got a folder-structure like this :
>
>/home/thomas/dev/modules/test1
>/home/thomas/dev/modules/test2
>
>they both contain two files:
>__init__.py
>handler.py
>
>The handler.py contains a class handlerclass, that's different for each of
>the handler files
>
>How can I import the handler.py module for each folder, be able to reload it
>and create object-instances from them?
>I want to be able to say 'mymodules.reload()' and reload them too.
>
>The point is to be able to easily create and import several modules into a
>server without stopping the server itself. I want to be able to create a
>folder in the /home/thomas/dev/modules-folder, put to files into it, call
>reload on the server and have the modules available inside the running
>server. I cannot know what the folder inside /home/thomas/dev/modules will
>be called, but it will contain a handler.py defining a handlerclass-object
>and a __init__.py ( if that's necessary ).
>
>Any clues or pointers?
>
>It's for use in medusa. I want to be able to dynamically define handlers
>that processes requests, and reload them.

I think you're interested in __import__ or the imp module.  This is untested,
off the top of my head.  You shouldn't need __init__.py for this.

class MyModules:
    def __init__(self, directory):
        self.directory = directory
        self.reload()
    def reload(self):
        self.handlers = []
        for fn in os.listdir(self.directory):
            if not fn.endswith('.py'):
                continue
            fn = os.path.join(self.directory, fn)
            try:
                self.handlers.append(imp.load_module((open(fn), fn,
                    ('.py', 'r', imp.PY_SOURCE))))
            except:
                log_error(sys.exc_info()[1])

    # for example
    def handler(self, data):
        for m in self.handlers:
            if m.can_handle(data):
                return m.handlerclass(data)
    def all_handlers(self):
        return [ h.__name__ for h in self.handlers ]

I don't remember if imp.load_module tries to cache in sys.modules, but if it
does, you may want to find a way that doesn't or remove it from the cache.



More information about the Python-list mailing list