-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_python.py
More file actions
107 lines (94 loc) · 3.47 KB
/
Copy pathdetect_python.py
File metadata and controls
107 lines (94 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""
Python Detection Script
Detects the correct Python command to use on the current system.
Handles pyenv, system Python, and other Python version managers.
"""
import sys
import subprocess
import os
import platform
def test_python_command(command):
"""Test if a Python command works and returns version info."""
try:
result = subprocess.run([command, '--version'],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
return True, result.stdout.strip()
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
pass
return False, None
def test_python_imports(command):
"""Test if a Python command can import required modules."""
try:
# Test basic imports that MushLog needs
test_script = """
import sys
import os
import tkinter
import sqlite3
print("Python {} at {}".format(sys.version, sys.executable))
"""
result = subprocess.run([command, '-c', test_script],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
return True, result.stdout.strip()
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
pass
return False, None
def detect_python_command():
"""Detect the best Python command to use."""
# Commands to try in order of preference
commands_to_try = [
'python', # Often points to pyenv-managed Python
'python3', # System Python 3
'python3.11', # Specific Python versions
'python3.10',
'python3.9',
'python3.8',
'python3.7'
]
# On macOS, also check for pyenv-specific paths
if platform.system() == 'Darwin':
# Check if pyenv is in PATH and get its Python
pyenv_python = None
try:
result = subprocess.run(['pyenv', 'which', 'python'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
pyenv_python = result.stdout.strip()
if pyenv_python and os.path.exists(pyenv_python):
commands_to_try.insert(0, pyenv_python)
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
pass
working_commands = []
for cmd in commands_to_try:
# First test if command exists and shows version
works, version_info = test_python_command(cmd)
if works:
# Then test if it can import required modules
imports_work, import_info = test_python_imports(cmd)
if imports_work:
working_commands.append({
'command': cmd,
'version_info': version_info,
'import_info': import_info
})
if not working_commands:
return None, "No working Python installation found"
# Return the first working command (preferred order)
best = working_commands[0]
return best['command'], f"Found Python: {best['version_info']} at {best['import_info']}"
def main():
"""Main function to detect and display Python command."""
command, message = detect_python_command()
if command:
print(f"✅ {message}")
print(f"Recommended command: {command}")
return command
else:
print(f"❌ {message}")
print("Please install Python 3.7 or higher")
return None
if __name__ == "__main__":
main()