5151platform  =  env .PioPlatform ()
5252projectconfig  =  env .GetProjectConfig ()
5353terminal_cp  =  locale .getpreferredencoding ().lower ()
54+ PYTHON_EXE  =  env .subst ("$PYTHONEXE" )  # Global Python executable path 
5455
5556# Framework directory path 
5657FRAMEWORK_DIR  =  platform .get_package_dir ("framework-arduinoespressif32" )
@@ -80,24 +81,20 @@ def add_to_pythonpath(path):
8081        sys .path .insert (0 , normalized_path )
8182
8283
83- def  setup_python_paths (env ):
84+ def  setup_python_paths ():
8485    """ 
8586    Setup Python paths based on the actual Python executable being used. 
86-      
87-     Args: 
88-         env: SCons environment object 
8987    """ 
90-     python_exe  =  env .subst ('$PYTHONEXE' )
91-     if  not  python_exe  or  not  os .path .isfile (python_exe ):
88+     if  not  PYTHON_EXE  or  not  os .path .isfile (PYTHON_EXE ):
9289        return 
9390
9491    # Get the directory containing the Python executable 
95-     python_dir  =  os .path .dirname (python_exe )
92+     python_dir  =  os .path .dirname (PYTHON_EXE )
9693    add_to_pythonpath (python_dir )
9794
9895    # Try to find site-packages directory using the actual Python executable 
9996    result  =  subprocess .run (
100-         [python_exe , "-c" , "import site; print(site.getsitepackages()[0])" ],
97+         [PYTHON_EXE , "-c" , "import site; print(site.getsitepackages()[0])" ],
10198        capture_output = True ,
10299        text = True ,
103100        timeout = 5 
@@ -108,7 +105,7 @@ def setup_python_paths(env):
108105            add_to_pythonpath (site_packages )
109106
110107# Setup Python paths based on the actual Python executable 
111- setup_python_paths (env )
108+ setup_python_paths ()
112109
113110
114111def  _get_executable_path (python_exe , executable_name ):
@@ -200,7 +197,7 @@ def install_python_deps():
200197        bool: True if successful, False otherwise 
201198    """ 
202199    # Get uv executable path 
203-     uv_executable  =  _get_uv_executable_path (env . subst ( "$PYTHONEXE" ) )
200+     uv_executable  =  _get_uv_executable_path (PYTHON_EXE )
204201
205202    try :
206203        result  =  subprocess .run (
@@ -216,7 +213,7 @@ def install_python_deps():
216213    if  not  uv_available :
217214        try :
218215            result  =  subprocess .run (
219-                 [env . subst ( "$PYTHONEXE" ) , "-m" , "pip" , "install" , "uv>=0.1.0" , "-q" , "-q" , "-q" ],
216+                 [PYTHON_EXE , "-m" , "pip" , "install" , "uv>=0.1.0" , "-q" , "-q" , "-q" ],
220217                capture_output = True ,
221218                text = True ,
222219                timeout = 30 ,  # 30 second timeout 
@@ -228,11 +225,11 @@ def install_python_deps():
228225                return  False 
229226
230227            # Update uv executable path after installation 
231-             uv_executable  =  _get_uv_executable_path (env . subst ( "$PYTHONEXE" ) )
228+             uv_executable  =  _get_uv_executable_path (PYTHON_EXE )
232229
233230            # Add Scripts directory to PATH for Windows 
234231            if  sys .platform  ==  "win32" :
235-                 python_dir  =  os .path .dirname (env . subst ( "$PYTHONEXE" ) )
232+                 python_dir  =  os .path .dirname (PYTHON_EXE )
236233                scripts_dir  =  os .path .join (python_dir , "Scripts" )
237234                if  os .path .isdir (scripts_dir ):
238235                    os .environ ["PATH" ] =  scripts_dir  +  os .pathsep  +  os .environ .get ("PATH" , "" )
@@ -297,7 +294,7 @@ def _get_installed_uv_packages():
297294
298295        cmd  =  [
299296            uv_executable , "pip" , "install" ,
300-             f"--python={ env . subst ( '$PYTHONEXE' ) }  " ,
297+             f"--python={ PYTHON_EXE }  " ,
301298            "--quiet" , "--upgrade" 
302299        ] +  packages_list 
303300
@@ -329,42 +326,37 @@ def _get_installed_uv_packages():
329326    return  True 
330327
331328
332- def  install_esptool (env ):
329+ def  install_esptool ():
333330    """ 
334331    Install esptool from package folder "tool-esptoolpy" using uv package manager. 
335332    Also determines the path to the esptool executable binary. 
336333     
337-     Args: 
338-         env: SCons environment object 
339-          
340334    Returns: 
341335        str: Path to esptool executable, or 'esptool' as fallback 
342336    """ 
343-     python_exe  =  env .subst ("$PYTHONEXE" )
344-     
345337    try :
346338        subprocess .check_call (
347-             [python_exe , "-c" , "import esptool" ], 
339+             [PYTHON_EXE , "-c" , "import esptool" ], 
348340            stdout = subprocess .DEVNULL , 
349341            stderr = subprocess .DEVNULL ,
350342            env = os .environ 
351343        )
352-         esptool_binary_path  =  _get_esptool_executable_path (python_exe )
344+         esptool_binary_path  =  _get_esptool_executable_path (PYTHON_EXE )
353345        return  esptool_binary_path 
354346    except  (subprocess .CalledProcessError , FileNotFoundError ):
355347        pass 
356348
357349    esptool_repo_path  =  env .subst (platform .get_package_dir ("tool-esptoolpy" ) or  "" )
358350    if  esptool_repo_path  and  os .path .isdir (esptool_repo_path ):
359-         uv_executable  =  _get_uv_executable_path (python_exe )
351+         uv_executable  =  _get_uv_executable_path (PYTHON_EXE )
360352        try :
361353            subprocess .check_call ([
362354                uv_executable , "pip" , "install" , "--quiet" ,
363-                 f"--python={ python_exe }  " ,
355+                 f"--python={ PYTHON_EXE }  " ,
364356                "-e" , esptool_repo_path 
365357            ], env = os .environ )
366358
367-             esptool_binary_path  =  _get_esptool_executable_path (python_exe )
359+             esptool_binary_path  =  _get_esptool_executable_path (PYTHON_EXE )
368360            return  esptool_binary_path 
369361
370362        except  subprocess .CalledProcessError  as  e :
@@ -376,7 +368,7 @@ def install_esptool(env):
376368
377369# Install Python dependencies and esptool 
378370install_python_deps ()
379- esptool_binary_path  =  install_esptool (env )
371+ esptool_binary_path  =  install_esptool ()
380372
381373
382374def  BeforeUpload (target , source , env ):
@@ -923,7 +915,7 @@ def firmware_metrics(target, source, env):
923915        return 
924916
925917    try :        
926-         cmd  =  [env . subst ( "$PYTHONEXE" ) , "-m" , "esp_idf_size" , "--ng" ]
918+         cmd  =  [PYTHON_EXE , "-m" , "esp_idf_size" , "--ng" ]
927919
928920        # Parameters from platformio.ini 
929921        extra_args  =  env .GetProjectOption ("custom_esp_idf_size_args" , "" )
@@ -1051,7 +1043,7 @@ def firmware_metrics(target, source, env):
10511043    env .Replace (
10521044        UPLOADER = join (FRAMEWORK_DIR , "tools" , "espota.py" ),
10531045        UPLOADERFLAGS = ["--debug" , "--progress" , "-i" , "$UPLOAD_PORT" ],
1054-         UPLOADCMD = '"$PYTHONEXE " "$UPLOADER" $UPLOADERFLAGS -f $SOURCE' ,
1046+         UPLOADCMD = f'" { PYTHON_EXE }  " "$UPLOADER" $UPLOADERFLAGS -f $SOURCE' ,
10551047    )
10561048    if  set (["uploadfs" , "uploadfsota" ]) &  set (COMMAND_LINE_TARGETS ):
10571049        env .Append (UPLOADERFLAGS = ["--spiffs" ])
0 commit comments