Skip to content

Windows: child Python has no Path after clear_python_envs(); subprocess powershell fails WinError 2 #2

@1w1w11w1

Description

@1w1w11w1

Summary

When PyAppify spawns the embedded Python process, clear_python_envs() removes the Path environment variable (listed in PYTHON_ENVS_TO_REMOVE). The launcher does not currently set a replacement Path/PATH for that child process.

Downstream Python code that uses subprocess.run(['powershell', ...]) (short executable name) then fails with [WinError 2] The system cannot find the file specified on Windows, because powershell.exe lives under %SystemRoot%\System32\WindowsPowerShell\v1.0\, which is not the same directory as %SystemRoot%\System32\ searched by default process creation rules.

This was observed with ok-ww (PyAppify + embedded Python + onnxocr-ppocrv5): onnxocr/check_npu.py calls PowerShell by name to query WMI for Intel NPU driver version; the exception is logged as Failed to check NPU driver version … use cpu instead.

The failure is not proof that NPU drivers are missing; it often means PowerShell could not be started under the stripped environment.

Reproduction

  1. Run any PyAppify-packaged app whose embedded Python eventually runs subprocess.run(['powershell', '-NoProfile', '-Command', '...']) without an absolute path to powershell.exe.
  2. Observe [WinError 2] when Path was removed and not restored for the Python child.

Root cause

  • src-tauri/src/utils/process.rs: PYTHON_ENVS_TO_REMOVE includes "Path"; clear_python_envs() calls env_remove for each.
  • src-tauri/src/execute_python.rs: after clear_python_envs(), only the small set of env vars from build_python_execution_environment is applied — no Path.
  • Windows: resolving the bare name powershell requires a Path that includes ...\WindowsPowerShell\v1.0\ (or an absolute path in the subprocess argv).

Expected behavior

Embedded Python child processes should have a minimal, intentional Path after stripping the host Path, e.g.:

  1. Embedded venv: python_home\Scripts, python_home (so packaged tools still resolve first).
  2. System dirs from the launcher process (still has SystemRoot): at least %SystemRoot%\System32, %SystemRoot%\System32\WindowsPowerShell\v1.0, %SystemRoot%\SysWOW64.

This preserves the original goal (avoid host Python shadowing the embedded runtime) while allowing standard Windows tools invoked by name.

Suggested fix (implementation sketch)

After cmd.clear_python_envs() in execute_python.rs (and similarly for pip / any other Command that uses clear_python_envs() on Windows), set Path and PATH to a string built from SystemRoot + embedded python_home as above.

Related downstream (optional hardening)

onnxocr-ppocrv5 check_npu.py uses ['powershell', ...]. Using the full path to %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe would reduce sensitivity to a narrow Path, but PyAppify should still restore a minimal Path for other subprocesses.

Environment

  • OS: Windows 10/11
  • PyAppify: current master behavior (PYTHON_ENVS_TO_REMOVE contains Path)
  • Symptom log line: Failed to check NPU driver version: [WinError 2] ... use cpu instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions