Cross-Platform Subprocess With Hidden Window

Cross-platform subprocess with hidden window

You can reduce one line :)

startupinfo = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)

Hide console with subprocess.Popen

stdout=subprocess.PIPE has no effect on whether or not a console appears. It just determines whether or not the stdout of the subprocess is captured in a pipe that you can read from or not.

shell=False is the default for all subprocess commands, so you don't need to provide it. It tells subprocess whether or not to use a shell to execute the provided command. It does not have any effect on whether or not a console appears on any platform.

creationflags = CREATE_NO_WINDOW will indeed hide the console on Windows (tested on Windows 7, assuming CREATE_NO_WINDOW == 0x08000000). It will cause an error to use it on non-Windows platforms, though. You should reference this question for a way to only provide creationflags on Windows, and also for alternative way to hide the console.

Note that the console appearing should only be an issue on Windows. On Posix platforms the console shouldn't ever appear when running subprocesses.

Executing subprocess from Python without opening Windows Command Prompt

How are you calling subprocess.check_call()? If you pass shell=True then the window should not be created as this will cause the SW_HIDE flag to be set for the STARTUPINFO.wShowWindow attribute.

Example:

subprocess.check_call(["ping", "google.com"], shell=True)

Cannot find STARTF_USESHOWWINDOW in python

STARTF_USESHOWWINDOW is a Windows specific flag and so you can read it out of win32con. If you don't have that module installed, you can simply define it for yourself.

hide console-window on executing _popen

You can call CreateProcess with the CREATE_NO_WINDOW
flag. This is obviously non portable but it does get the job done.

Subprocess.Popen() : hide the cmd shell

This is a typical problem Python-programmers encounter - and therefore, a solution is offered by Python itself. It has been asked on SO many times, e.g., here, but for you, the problem is a little more complicated.

It's all about whether you use python.exe or pythonw.exe to run your script. For the first one, a console is opened, for the second it's not.

As you use compiled scripts, you have to tell "the compiler" which version you want to use. Assuming you are using py2exe, you can have a look at this post on SO. Here it is explained in detail how to proceed.

How to prevent .sh file from opening new console using python subprocess

My own solution:

As mentioned, the problem with opening a new window didn't occur in git bash. In the end all I needed to do was add git bash to the PATH environment variable and add "bash" at the start of the command like so:

command = ["bash", os.path.join("tools", "unmunch.sh"), dic_path, aff_path]

This meant a new window did not open, and therefore the output was written to the desired output file.



Related Topics



Leave a reply



Submit