Return if the given module path could be imported normally by Python, akin to the user entering the REPL and importing the corresponding module name directly, and corresponds to the module_path specified. :param module_name: Full module name that we want to check if is impo
(module_name: str, module_path: Path)
| 889 | |
| 890 | |
| 891 | def is_importable(module_name: str, module_path: Path) -> bool: |
| 892 | """ |
| 893 | Return if the given module path could be imported normally by Python, akin to the user |
| 894 | entering the REPL and importing the corresponding module name directly, and corresponds |
| 895 | to the module_path specified. |
| 896 | |
| 897 | :param module_name: |
| 898 | Full module name that we want to check if is importable. |
| 899 | For example, "app.models". |
| 900 | |
| 901 | :param module_path: |
| 902 | Full path to the python module/package we want to check if is importable. |
| 903 | For example, "/projects/src/app/models.py". |
| 904 | """ |
| 905 | try: |
| 906 | # Note this is different from what we do in ``_import_module_using_spec``, where we explicitly search through |
| 907 | # sys.meta_path to be able to pass the path of the module that we want to import (``meta_importer.find_spec``). |
| 908 | # Using importlib.util.find_spec() is different, it gives the same results as trying to import |
| 909 | # the module normally in the REPL. |
| 910 | spec = importlib.util.find_spec(module_name) |
| 911 | except (ImportError, ValueError, ImportWarning): |
| 912 | return False |
| 913 | else: |
| 914 | return spec_matches_module_path(spec, module_path) |
| 915 | |
| 916 | |
| 917 | def compute_module_name(root: Path, module_path: Path) -> str | None: |