在 windows 上通过 python 调用使用 linux 子系统编译的程序

Invoking program compiled using linux subsytem through python on windows

我在Windows中介绍的Linux子系统中编译了一个程序 10. 如何使用python子进程命令调用它?默认情况下,子进程命令似乎使用 windows shell 而不是 linux 子系统

这是一个示例程序(请记住,当我通过 windows python anaconda 解释器 运行 它时,我确实安装了 linux 子系统

import subprocess
subprocess.Popen('ls', shell=False)

导致错误:

Traceback (most recent call last):
  File "C:\Users\rt\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-cf9f6642ea11>", line 1, in <module>
    subprocess.Popen('ls', shell=False)
  File "C:\Users\rt\Anaconda3\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\rt\Anaconda3\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

subprocess 在 Windows 上使用 CreateProcess 到 运行 个程序。如果您使用 shell=True 它会将 cmd.exe 添加到您的命令的前面,以便它是 运行 通过 Windows 命令解释器而不是直接执行。您可以使用 Windows 子系统玩 Linux 的相同游戏。它使用 wsl.exe 很像 cmd.exe。因此,使用 shell=False(默认值),您可以

Popen("wsl.exe " + "my_command param1 param2")

Popen(["wsl.exe"] + ["my_command", "param1", "param2"])