Find module `module_name` on sys.path, and return the path to module `module_name`. * If `module_name` refers to a module directory, then return path to `__init__` file. * If `module_name` is a directory without an __init__file, return None. * If module is missing or does not
(module_name: str)
| 41 | #----------------------------------------------------------------------------- |
| 42 | |
| 43 | def find_mod(module_name: str) -> str | None | importlib.abc.Loader: |
| 44 | """ |
| 45 | Find module `module_name` on sys.path, and return the path to module `module_name`. |
| 46 | |
| 47 | * If `module_name` refers to a module directory, then return path to `__init__` file. |
| 48 | * If `module_name` is a directory without an __init__file, return None. |
| 49 | |
| 50 | * If module is missing or does not have a `.py` or `.pyw` extension, return None. |
| 51 | * Note that we are not interested in running bytecode. |
| 52 | |
| 53 | * Otherwise, return the fill path of the module. |
| 54 | |
| 55 | Parameters |
| 56 | ---------- |
| 57 | module_name : str |
| 58 | |
| 59 | Returns |
| 60 | ------- |
| 61 | module_path : str |
| 62 | Path to module `module_name`, its __init__.py, or None, |
| 63 | depending on above conditions. |
| 64 | """ |
| 65 | spec = importlib.util.find_spec(module_name) |
| 66 | if spec is None: |
| 67 | return None |
| 68 | module_path = spec.origin |
| 69 | if module_path is None: |
| 70 | if spec.loader is not None and spec.loader in sys.meta_path: |
| 71 | return spec.loader |
| 72 | return None |
| 73 | else: |
| 74 | split_path = module_path.split(".") |
| 75 | if split_path[-1] in ["py", "pyw"]: |
| 76 | return module_path |
| 77 | else: |
| 78 | return None |