parameters: (path, forceload=0, cache={})
return_statement: return module
docstring: 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).
docstring_summary: Import a module; handle errors; return None if the module isn't found.
docstring_tokens: Import a module ; handle errors ; return None if the module isn t found .
function: 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:
                  # Avoid simply calling reload() because it leaves names in
                  # the currently loaded module lying around if they're not
                  # defined in the new source file.  Instead, remove the
                  # module from sys.modules and re-import.  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 exc is ImportError and extract_tb(tb)[-1][2]=='safeimport':
              # The import error occurred directly in this function,
              # which means there is 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 split(path, '.')[1:]:
          try: module = getattr(module, part)
          except AttributeError: return None
      return module
function_tokens: 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 : # Avoid simply calling reload() because it leaves names in # the currently loaded module lying around if they're not # defined in the new source file.  Instead, remove the # module from sys.modules and re-import.  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 exc is ImportError and extract_tb ( tb ) [ - 1 ] [ 2 ] == 'safeimport' : # The import error occurred directly in this function, # which means there is 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 split ( path , '.' ) [ 1 : ] : try : module = getattr ( module , part ) except AttributeError : return None return module