python - How to import a module given the full path? - Stack ...
stackoverflow.com › questions › 67631But you can also give a Path and/or a glob to select some specific files. from pathlib import Path from importlib.util import spec_from_file_location, module_from_spec from typing import Optional def get_module_from_path(path: Path, relative_to: Optional[Path] = None): if not relative_to: relative_to = Path.cwd() abs_path = path.absolute() relative_path = abs_path.relative_to(relative_to.absolute()) if relative_path.name == "__init__.py": relative_path = relative_path.parent module_name ...