問題描述
帶有 python 子進程的 WSL (WSL with python subprocess)
我正在嘗試使用 python 子進程在 WSL2(Linux 的 Windows 子系統)上執行命令。
從 powershell 和 cmd 命令 wsl ‑l
正在運行。
當我像這樣使用 python 子進程時:
import subprocess
subprocess.Popen(['wsl', '‑l'], shell=True)
我收到命令“wsl”的錯誤。拼寫錯誤或找不到。(對於德國人,我很抱歉,但這是原始錯誤消息:Der Befehl “wsl”ist entweder falsch geschrieben oder konnte nicht gefunden werden。
)
我也在此處應用了故障排除部分中列出的修復https:/ /文檔。
參考解法
方法 1:
i'm kind of a newbie, but i could successfully run the following code for opening wsl from the windows prompt:
subprocess.run(['wsl'], shell = True)