diff --git a/HISTORY.rst b/HISTORY.rst index 2ed70050..b87270c6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -19,6 +19,7 @@ PlatformIO Core 6 ~~~~~~~~~~~~~~~~~~~ * Resolved an issue that caused generated projects for `PlatformIO IDE for VSCode `__ to break when the ``-iprefix`` compiler flag was used +* Resolved an issue encountered while utilizing the `pio pkg exec `__ command on the Windows platform to execute Python scripts from a package 6.1.9 (2023-07-06) ~~~~~~~~~~~~~~~~~~ diff --git a/platformio/package/commands/exec.py b/platformio/package/commands/exec.py index fc100f19..41386d5e 100644 --- a/platformio/package/commands/exec.py +++ b/platformio/package/commands/exec.py @@ -20,7 +20,7 @@ import click from platformio.compat import IS_MACOS, IS_WINDOWS from platformio.exception import ReturnErrorCode, UserSideException from platformio.package.manager.tool import ToolPackageManager -from platformio.proc import get_pythonexe_path +from platformio.proc import get_pythonexe_path, where_is_program @click.command("exec", short_help="Run command from package tool") @@ -52,9 +52,13 @@ def package_exec_cmd(obj, package, call, args): inject_pkg_to_environ(pkg) os.environ["PIO_PYTHON_EXE"] = get_pythonexe_path() + # inject current python interpreter on Windows - if IS_WINDOWS and args and args[0].endswith(".py"): + if args[0].endswith(".py"): args = [os.environ["PIO_PYTHON_EXE"]] + list(args) + if not os.path.exists(args[1]): + args[1] = where_is_program(args[1]) + result = None try: run_options = dict(shell=call is not None, env=os.environ) diff --git a/platformio/proc.py b/platformio/proc.py index fb66bc7f..acfce4d9 100644 --- a/platformio/proc.py +++ b/platformio/proc.py @@ -185,10 +185,17 @@ def copy_pythonpath_to_osenv(): def where_is_program(program, envpath=None): - env = os.environ + env = os.environ.copy() if envpath: env["PATH"] = envpath + # look up in $PATH + for bin_dir in env.get("PATH", "").split(os.pathsep): + if os.path.isfile(os.path.join(bin_dir, program)): + return os.path.join(bin_dir, program) + if IS_WINDOWS and os.path.isfile(os.path.join(bin_dir, "%s.exe" % program)): + return os.path.join(bin_dir, "%s.exe" % program) + # try OS's built-in commands try: result = exec_command(["where" if IS_WINDOWS else "which", program], env=env) @@ -197,13 +204,6 @@ def where_is_program(program, envpath=None): except OSError: pass - # look up in $PATH - for bin_dir in env.get("PATH", "").split(os.pathsep): - if os.path.isfile(os.path.join(bin_dir, program)): - return os.path.join(bin_dir, program) - if os.path.isfile(os.path.join(bin_dir, "%s.exe" % program)): - return os.path.join(bin_dir, "%s.exe" % program) - return program