Files
troupe-standup/wizard-work/spark/python_env_check.py

69 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Quick Python environment validator.
Checks pip, site-packages accessibility, and common import paths.
"""
import sys
import os
import importlib.util
def check_pip():
"""Verify pip is accessible."""
try:
import pip
return True, "pip available"
except ImportError:
try:
import ensurepip
return True, "pip can be bootstrapped via ensurepip"
except ImportError:
return False, "pip not available and cannot bootstrap"
def check_site_packages():
"""Check site-packages directory exists and is writable."""
import site
paths = site.getsitepackages()
for p in paths:
if os.path.isdir(p):
writable = os.access(p, os.W_OK)
return True, f"site-packages found at {p}, writable={writable}"
return False, "no site-packages directory found"
def check_stdlib_imports():
"""Test critical stdlib modules."""
modules = ['os', 'sys', 'json', 'urllib.request', 'pathlib', 'typing']
failed = []
for mod in modules:
try:
importlib.import_module(mod)
except ImportError as e:
failed.append((mod, str(e)))
return len(failed) == 0, failed if failed else "all stdlib modules importable"
def main():
print(f"Python environment check (py{sys.version_info.major}.{sys.version_info.minor})")
print(f"Executable: {sys.executable}")
print(f"Site-packages: {next((p for p in __import__('site').getsitepackages() if os.path.isdir(p)), 'none')}\n")
checks = [
("pip", check_pip),
("site-packages", check_site_packages),
("stdlib imports", check_stdlib_imports),
]
results = []
for name, func in checks:
passed, msg = func()
results.append((name, passed, msg))
all_passed = all(p for _, p, _ in results)
for name, passed, msg in results:
status = "PASS" if passed else "FAIL"
print(f"[{status}] {name}: {msg}")
print(f"\n{'All checks passed!' if all_passed else 'Some checks failed.'}")
return 0 if all_passed else 1
if __name__ == '__main__':
sys.exit(main())