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)
| 844 | |
| 845 | @functools.cache |
| 846 | def 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 | |
| 884 | def compute_search_paths( |
no test coverage detected
searching dependent graphs…