class AddPath: def __init__(self, posix_path): self.path = str(posix_path) def __enter__(self): sys.path.insert(0, self.path) def __exit__(self, exc_type, exc_value, traceback): try: sys.path.remove(self.path) except ValueError: pass with AddPath(Path(os.getcwd()).parent / 'pa_core'): pa_core = __import__('pa_core')