Check if a module exists and return its path. This function searches for a module by navigating through the module hierarchy in each path of sys.path, checking for both .py files and packages with __init__.py. Args: module_name: The name of the module to search for (e.g., "pack
(module_name: str)
| 11 | |
| 12 | |
| 13 | def get_module_path(module_name: str) -> Path | None: |
| 14 | """Check if a module exists and return its path. |
| 15 | |
| 16 | This function searches for a module by navigating through the module hierarchy |
| 17 | in each path of sys.path, checking for both .py files and packages with __init__.py. |
| 18 | |
| 19 | Args: |
| 20 | module_name: The name of the module to search for (e.g., "package.submodule"). |
| 21 | |
| 22 | Returns: |
| 23 | The path to the module file if found, None otherwise. |
| 24 | """ |
| 25 | parts = module_name.split(".") |
| 26 | |
| 27 | # Check each path in sys.path |
| 28 | for path in sys.path: |
| 29 | current_path = Path(path) |
| 30 | |
| 31 | # Navigate through the module hierarchy |
| 32 | for i, part in enumerate(parts): |
| 33 | potential_file = current_path / (part + ".py") |
| 34 | potential_dir = current_path / part |
| 35 | |
| 36 | if potential_file.is_file(): |
| 37 | # We encountered a file, but we can't continue deeper |
| 38 | if i == len(parts) - 1: |
| 39 | return potential_file |
| 40 | return None # Can't continue deeper |
| 41 | if potential_dir.is_dir(): |
| 42 | # It's a package, so we can continue deeper |
| 43 | current_path = potential_dir |
| 44 | else: |
| 45 | break # Path doesn't exist, break out of the loop |
| 46 | else: |
| 47 | return current_path / "__init__.py" # Made it through all parts |
| 48 | |
| 49 | return None |
| 50 | |
| 51 | |
| 52 | async def run_in_thread(func: Callable) -> Any: |
no test coverage detected