def simple_import(path_to_module): err_prefix = "Importing '%s' failed: " % path_to_module if not os.path.exists(path_to_module): raise DataError(err_prefix + 'File does not exist') moddir, modname = _split_path_to_module(path_to_module) try: try: module = __import__(modname) if normpath(moddir) != normpath(os.path.dirname(module.__file__)): del sys.modules[modname] module = __import__(modname) except: raise DataError(err_prefix + get_error_message()) finally: sys.path.pop(0) return module
def _get_pathname(target, base): target = normpath(target) base = normpath(base) if os.path.isfile(base): base = os.path.dirname(base) if base == target: return os.path.basename(target) base_drive, base_path = os.path.splitdrive(base) # if in Windows and base and link on different drives if os.path.splitdrive(target)[0] != base_drive: return target common_len = len(_common_path(base, target)) if base_path == os.sep: return target[common_len:] if common_len == len(base_drive) + len(os.sep): common_len -= len(os.sep) dirs_up = os.sep.join([os.pardir] * base[common_len:].count(os.sep)) return os.path.join(dirs_up, target[common_len + len(os.sep):])
def _get_module_source(module): try: source = module.__file__ if not source: raise AttributeError except AttributeError: # Java classes not packaged in a jar file do not have __file__. return '<unknown>' dirpath, filename = os.path.split(os.path.abspath(source)) return os.path.join(normpath(dirpath), filename)