2

I have a program I compiled in the Linux subsystem introduced in Windows 10. How can I use the python subprocess command to invoke it? By default the subprocess command seems to use the windows shell and not the linux subsytem

Here is a sample program (remember that while I am running it through a windows python anaconda interpretor), I do have linux subsytem installed

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

results in error:

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
11
  • In a way similar to however you would run it from the command line. Can you do that? Commented May 2, 2018 at 22:13
  • @martineau, I can run it from command line using ./name_of_program. The issue is that the python interpretor is on windows Commented May 2, 2018 at 22:32
  • 1
    Can you post the subprocess call? subprocess.Popen("./name_of_program", shell=False) should run it through Windows CreateProcess. Commented May 2, 2018 at 22:33
  • If you can run it from the command line by just giving the name of the file, you should also be able to run it via subprocess—which is what @tdelaney just said I believe. Commented May 2, 2018 at 22:35
  • 1
    Microsoft says wsl.exe command learn.microsoft.com/en-us/windows/wsl/…. Commented May 2, 2018 at 22:53

1 Answer 1

1

subprocess uses CreateProcess to run programs on Windows. If you use shell=True it adds cmd.exe to the front of your command so that it is run through Windows command interpreter instead of direct execution. You can play the same game with the Windows Subsystem for Linux. It uses wsl.exe much like cmd.exe. So, with shell=False (the default) you can

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

or

Popen(["wsl.exe"] + ["my_command", "param1", "param2"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.