MCPcopy
hub / github.com/pytest-dev/pytest / is_importable

Function is_importable

src/_pytest/pathlib.py:891–914  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

889
890
891def 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
917def compute_module_name(root: Path, module_path: Path) -> str | None:

Callers 2

test_is_importableFunction · 0.90

Calls 2

spec_matches_module_pathFunction · 0.85
find_specMethod · 0.45

Tested by 1

test_is_importableFunction · 0.72