(path, forceload=0, cache={})
return module
Import a module; handle errors; return None if the module isn't found.

    If the module *is* found but an exception occurs, it's wrapped in an
    ErrorDuringImport exception and reraised.  Unlike __import__, if a
    package path is specified, the module at the end of the path is returned,
    not the package at the beginning.  If the optional 'forceload' argument
    is 1, we reload the module from disk (unless it's a dynamic extension).
Import a module; handle errors; return None if the module isn't found.
Import a module ; handle errors ; return None if the module isn t found .
def safeimport(path, forceload=0, cache={}):
    """Import a module; handle errors; return None if the module isn't found.

    If the module *is* found but an exception occurs, it's wrapped in an
    ErrorDuringImport exception and reraised.  Unlike __import__, if a
    package path is specified, the module at the end of the path is returned,
    not the package at the beginning.  If the optional 'forceload' argument
    is 1, we reload the module from disk (unless it's a dynamic extension)."""
    try:
        # If forceload is 1 and the module has been previously loaded from
        # disk, we always have to reload the module.  Checking the file's
        # mtime isn't good enough (e.g. the module could contain a class
        # that inherits from another module that has changed).
        if forceload and path in sys.modules:
            if path not in sys.builtin_module_names:
                # Remove the module from sys.modules and re-import to try
                # and avoid problems with partially loaded modules.
                # Also remove any submodules because they won't appear
                # in the newly loaded module's namespace if they're already
                # in sys.modules.
                subs = [m for m in sys.modules if m.startswith(path + '.')]
                for key in [path] + subs:
                    # Prevent garbage collection.
                    cache[key] = sys.modules[key]
                    del sys.modules[key]
        module = __import__(path)
    except:
        # Did the error occur before or after the module was found?
        (exc, value, tb) = info = sys.exc_info()
        if path in sys.modules:
            # An error occurred while executing the imported module.
            raise ErrorDuringImport(sys.modules[path].__file__, info)
        elif exc is SyntaxError:
            # A SyntaxError occurred before we could execute the module.
            raise ErrorDuringImport(value.filename, info)
        elif issubclass(exc, ImportError) and value.name == path:
            # No such module in the path.
            return None
        else:
            # Some other error occurred during the importing process.
            raise ErrorDuringImport(path, sys.exc_info())
    for part in path.split('.')[1:]:
        try: module = getattr(module, part)
        except AttributeError: return None
    return module
def safeimport ( path , forceload = 0 , cache = { } ) : try : # If forceload is 1 and the module has been previously loaded from # disk, we always have to reload the module.  Checking the file's # mtime isn't good enough (e.g. the module could contain a class # that inherits from another module that has changed). if forceload and path in sys . modules : if path not in sys . builtin_module_names : # Remove the module from sys.modules and re-import to try # and avoid problems with partially loaded modules. # Also remove any submodules because they won't appear # in the newly loaded module's namespace if they're already # in sys.modules. subs = [ m for m in sys . modules if m . startswith ( path + '.' ) ] for key in [ path ] + subs : # Prevent garbage collection. cache [ key ] = sys . modules [ key ] del sys . modules [ key ] module = __import__ ( path ) except : # Did the error occur before or after the module was found? ( exc , value , tb ) = info = sys . exc_info ( ) if path in sys . modules : # An error occurred while executing the imported module. raise ErrorDuringImport ( sys . modules [ path ] . __file__ , info ) elif exc is SyntaxError : # A SyntaxError occurred before we could execute the module. raise ErrorDuringImport ( value . filename , info ) elif issubclass ( exc , ImportError ) and value . name == path : # No such module in the path. return None else : # Some other error occurred during the importing process. raise ErrorDuringImport ( path , sys . exc_info ( ) ) for part in path . split ( '.' ) [ 1 : ] : try : module = getattr ( module , part ) except AttributeError : return None return module