When we are asked to import a module, if we get an import error and the
    calling script is one we are serving (not one in the python libraries), try
    again in the same directory as the script that is calling import.
        It seems like we should use sys.meta_path and combine our path with the
    path sent to imp.find_module.  This requires duplicating a bunch of logic
    from the imp module and is actually heavier than this technique.

    :params: see __builtin__.__import__

def tangelo_import(*args, **kwargs):
    """
    When we are asked to import a module, if we get an import error and the
    calling script is one we are serving (not one in the python libraries), try
    again in the same directory as the script that is calling import.
        It seems like we should use sys.meta_path and combine our path with the
    path sent to imp.find_module.  This requires duplicating a bunch of logic
    from the imp module and is actually heavier than this technique.

    :params: see __builtin__.__import__
    """
    try:
        return builtin_import(*args, **kwargs)
    except ImportError:
        if not hasattr(cherrypy.thread_data, "modulepath"):
            raise
        path = os.path.abspath(cherrypy.thread_data.modulepath)
        root = os.path.abspath(cherrypy.config.get("webroot"))
        result = None
        imp.acquire_lock()
        oldpath = sys.path
        try:
            # If the module's path isn't in the system path but is in our
            # serving area, temporarily add it and try the import again.
            if path not in sys.path and (path == root or path.startswith(root + os.path.sep)):
                sys.path = [path] + sys.path
                result = builtin_import(*args, **kwargs)
        finally:
            sys.path = oldpath
            imp.release_lock()
        if result is not None:
            return result
        raise