Skip to content

Commit aa4a13b

Browse files
authored
Use SCons env var for Python path
1 parent a464095 commit aa4a13b

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

builder/main.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,49 @@ def add_to_pythonpath(path):
6868
# Framework directory path
6969
FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoespressif32")
7070

71-
# Add framework Python tools to path if available
72-
if FRAMEWORK_DIR:
73-
framework_python_path = os.path.join(FRAMEWORK_DIR, "tools", "python")
74-
if os.path.isdir(framework_python_path):
75-
add_to_pythonpath(framework_python_path)
71+
def setup_python_paths(env):
72+
"""
73+
Setup Python paths based on the actual Python executable being used.
74+
75+
Args:
76+
env: SCons environment object
77+
"""
78+
python_exe = env.subst('$PYTHONEXE')
79+
if not python_exe or not os.path.isfile(python_exe):
80+
return
81+
82+
# Get the directory containing the Python executable
83+
python_dir = os.path.dirname(python_exe)
84+
add_to_pythonpath(python_dir)
85+
86+
# Try to find site-packages directory using the actual Python executable
87+
try:
88+
result = subprocess.run(
89+
[python_exe, "-c", "import site; print(site.getsitepackages()[0])"],
90+
capture_output=True,
91+
text=True,
92+
timeout=5
93+
)
94+
if result.returncode == 0:
95+
site_packages = result.stdout.strip()
96+
if os.path.isdir(site_packages):
97+
add_to_pythonpath(site_packages)
98+
except (subprocess.TimeoutExpired, FileNotFoundError, Exception):
99+
# Fallback: try common site-packages locations
100+
possible_paths = [
101+
os.path.join(python_dir, "Lib", "site-packages"), # Windows
102+
os.path.join(python_dir, "..", "lib", f"python{sys.version_info.major}.{sys.version_info.minor}", "site-packages"), # Unix
103+
]
104+
105+
for path in possible_paths:
106+
normalized_path = os.path.normpath(path)
107+
if os.path.isdir(normalized_path):
108+
add_to_pythonpath(normalized_path)
109+
break
110+
111+
# Setup Python paths based on the actual Python executable
112+
setup_python_paths(env)
113+
76114

77115
# Python dependencies required for the build process
78116
python_deps = {

0 commit comments

Comments
 (0)