Loads a python module from string

def load_python_object(name):
    """
    Loads a python module from string
    """
    logger = getLoggerWithNullHandler('commando.load_python_object')
    (module_name, _, object_name) = name.rpartition(".")
    if module_name == '':
        (module_name, object_name) = (object_name, module_name)
    try:
        logger.debug('Loading module [%s]' % module_name)
        module = __import__(module_name)
    except ImportError:
        raise CommandoLoaderException(
                "Module [%s] cannot be loaded." % module_name)

    if object_name == '':
        return module

    try:
        module = sys.modules[module_name]
    except KeyError:
        raise CommandoLoaderException(
                "Error occured when loading module [%s]" % module_name)

    try:
        logger.debug('Getting object [%s] from module [%s]' %
                     (object_name, module_name))
        return getattr(module, object_name)
    except AttributeError:
        raise CommandoLoaderException(
                "Cannot load object [%s]. "
                "Module [%s] does not contain object [%s]. "
                "Please fix the configuration or "
                "ensure that the module is installed properly" % (
                    name,
                    module_name,
                    object_name))