Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does python subprocess.Popen launch a subprocess through cmd.exe?

I call subprocess like this:

command = 'c:\somepath\myexe.exe'

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

p = subprocess.Popen(command, shell=False, startupinfo=startupinfo)

It appears that instead of launching myexe.exe directly, it goes through cmd.exe. Can I avoid cmd.exe here?

like image 443
pbx Avatar asked Nov 08 '11 19:11

pbx


People also ask

How does subprocess Popen work?

Popen FunctionThe function should return a pointer to a stream that may be used to read from or write to the pipe while also creating a pipe between the calling application and the executed command. Immediately after starting, the Popen function returns data, and it does not wait for the subprocess to finish.

How do I use subprocess Popen in Python?

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.

What is the difference between subprocess run and subprocess Popen?

The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen. communicate() yourself to pass and receive data to your process.

What does Popen do Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.


1 Answers

Iff you're using Python 2.7 - use subprocess._subprocess.STARTF_USESHOWWINDOW instead of subprocess.STARTF_USESHOWWINDOW. I think this would solve it.

like image 159
Sushant Khurana Avatar answered Nov 14 '22 17:11

Sushant Khurana