(module, report)
| 870 | |
| 871 | |
| 872 | def install_python_module(module, report): |
| 873 | python_cmd = shutil.which("python") or sys.executable |
| 874 | candidates = python_candidates(module) |
| 875 | errors = [] |
| 876 | for package in candidates: |
| 877 | print(f"[*] Python import '{module}' missing -> trying pip package '{package}'") |
| 878 | code, out, err = run( |
| 879 | [python_cmd, "-m", "pip", "install", "--upgrade", package], |
| 880 | capture=True, |
| 881 | ) |
| 882 | output = (out + "\n" + err).strip() |
| 883 | if code == 0 and python_module_available(module, refresh=True): |
| 884 | report.line("installed", f"Python import '{module}' supplied by '{package}'") |
| 885 | return True |
| 886 | |
| 887 | errors.append(f"{package}: {output[-500:]}") |
| 888 | native_markers = ( |
| 889 | "failed building wheel", |
| 890 | "could not build wheels", |
| 891 | "error: command 'clang'", |
| 892 | "cargo", |
| 893 | "rust compiler", |
| 894 | "pkg-config", |
| 895 | "openssl", |
| 896 | "ffi.h", |
| 897 | ) |
| 898 | if any(marker in output.lower() for marker in native_markers): |
| 899 | print("[*] Native build dependency failure detected; installing build tools and retrying.") |
| 900 | extra_packages = PYTHON_SYSTEM_DEPENDENCIES.get(package, []) + PYTHON_SYSTEM_DEPENDENCIES.get(module, []) |
| 901 | pkg_install(PYTHON_NATIVE_BUILD_PACKAGES + extra_packages) |
| 902 | code, out, err = run( |
| 903 | [python_cmd, "-m", "pip", "install", "--upgrade", "--no-build-isolation", package], |
| 904 | capture=True, |
| 905 | ) |
| 906 | output = (out + "\n" + err).strip() |
| 907 | if code == 0 and python_module_available(module, refresh=True): |
| 908 | report.line("fixed", f"Python import '{module}' installed as '{package}' after adding build dependencies") |
| 909 | return True |
| 910 | errors.append(f"{package} retry: {output[-500:]}") |
| 911 | |
| 912 | report.line( |
| 913 | "failures", |
| 914 | f"Python import '{module}' unresolved. Tried: {', '.join(candidates)}. Last error: {errors[-1] if errors else 'unknown'}", |
| 915 | ) |
| 916 | return False |
| 917 | |
| 918 | |
| 919 | def python_module_is_local(module, path, scan_root): |
no test coverage detected