Error Handling

Logging

Pluginlib uses the logging module from the Python Standard Library for debug and info level messages. The ‘pluginlib’ logging.Logger instance is configured with a NullHandler so, unless logging is configured in the program, there will be no output.

To configure basic debug logging in a program:

import logging

logging.basicConfig(level=logging.DEBUG)

See the logging module for more information on configuring logging.

To disable all logging for Pluginlib:

import logging

logging.getLogger('pluginlib').propagate = False

To change the logging level for Pluginlib only:

import logging

logging.getLogger('pluginlib').setLevel(logging.DEBUG)

Warnings

Pluginlib raises a custom PluginWarning warning for malformed plugins and a custom EntryPointWarning warning for invalid entry points. EntryPointWarning is a subclass of the builtin ImportWarning and ignored by default in Python.

To raise an exception when a PluginWarning occurs:

import warnings
import pluginlib

warnings.simplefilter('error', pluginlib.PluginWarning)

See the warnings module for more information on warnings.

Exceptions

When PluginLoader encounters an error importing a module, a PluginImportError exception will be raised. PluginImportError is a subclass of PluginlibError.

When possible, PluginImportError.friendly is populated with a formatted exception truncated to limit the output to the relevant part of the traceback.

To use friendly output for import errors:

import sys
import pluginlib

loader = pluginlib.PluginLoader(modules=['sample_plugins'])

try:
    plugins = loader.plugins
except pluginlib.PluginImportError as e:
    if e.friendly:
        sys.exit(e.friendly)
    else:
        raise