pehrs pehrs - 1 year ago 130
Python Question

Dynamic class loading in Python 2.6: RuntimeWarning: Parent module 'plugins' not found while handling absolute import

I am working on a plugin system where plugin modules are loaded like this:

def load_plugins():
for p in plugins:
log.debug("Possible plugin: %s", name)
f, file, desc=imp.find_module(name, ["plugins"])
plugin=imp.load_module('plugins.'+name, f, file, desc)
getattr(plugin, "__init__")(log)
except Exception as e:"Failed to load plugin: "+str(p))"Error: %s " % (e))
return instances

The code works, but for each import statement in the plugin code i get a warning like this:

plugins/ RuntimeWarning: Parent module 'plugins' not found while handling absolute import
import os

No errors are reported for the main program code, and the plugins work.

Can somebody explain what the warning means and what I doing wrong. Do I need to create an empty plugins module separately and import it to keep python happy?

Answer Source

If the directory plugins were a real package (contained fine), you could easily use pkgutils to enumerate its plugin files and load them.

import pkgutil
# import our package
import plugins

However, it can work without a plugin package anyway, try this:

import pkgutil

Also it is possible to make a package that only exists at runtime:

import types
import sys
plugins = types.ModuleType("plugins")
plugins.__path__ = ["plugins"]

sys.modules["plugins"] = plugins
import plugins.testplugin

However that hack that was mostly for fun!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download