MCPcopy Index your code
hub / github.com/python/mypy / get_search_dirs

Function get_search_dirs

mypy/modulefinder.py:846–881  ·  view source on GitHub ↗

Find package directories for given python. Guaranteed to return absolute paths. This runs a subprocess call, which generates a list of the directories in sys.path. To avoid repeatedly calling a subprocess (which can be slow!) we lru_cache the results.

(python_executable: str | None)

Source from the content-addressed store, hash-verified

844
845@functools.cache
846def get_search_dirs(python_executable: str | None) -> tuple[list[str], list[str]]:
847 """Find package directories for given python. Guaranteed to return absolute paths.
848
849 This runs a subprocess call, which generates a list of the directories in sys.path.
850 To avoid repeatedly calling a subprocess (which can be slow!) we
851 lru_cache the results.
852 """
853
854 if python_executable is None:
855 return ([], [])
856 elif python_executable == sys.executable:
857 # Use running Python's package dirs
858 sys_path, site_packages = pyinfo.getsearchdirs()
859 else:
860 # Use subprocess to get the package directory of given Python
861 # executable
862 env = {**dict(os.environ), "PYTHONSAFEPATH": "1"}
863 try:
864 sys_path, site_packages = ast.literal_eval(
865 subprocess.check_output(
866 [python_executable, pyinfo.__file__, "getsearchdirs"],
867 env=env,
868 stderr=subprocess.PIPE,
869 ).decode()
870 )
871 except subprocess.CalledProcessError as err:
872 print(err.stderr)
873 print(err.stdout)
874 raise
875 except OSError as err:
876 assert err.errno is not None
877 reason = os.strerror(err.errno)
878 raise CompileError(
879 [f"mypy: Invalid python executable '{python_executable}': {reason}"]
880 ) from err
881 return sys_path, site_packages
882
883
884def compute_search_paths(

Callers 2

process_optionsFunction · 0.90
compute_search_pathsFunction · 0.85

Calls 4

CompileErrorClass · 0.90
dictClass · 0.85
printFunction · 0.85
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…