python calling external programs without opening console (dos prompt) [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running a process in pythonw with Popen without a console
How do I eliminate Windows consoles from spawned processes in Python (2.7)?
I am using Python 2.7 and running the python scripts from within IDLE.
The commands I am executing are simple exe's that perform quick tasks. The issue I am having is every time the external commands are called from within Python a console is created and it flashes on my screen and takes focus, thus preventing me from using my PC while executing various scripts.
Examples of how I am calling them from within Python are as follows:
result = call(["Commands\Set.exe", str(i), ARG2])
check_output(["Commands\Read.exe", ARG2])
Searching for a solution I came across adding the following
shell=True
to make the following command
check_output(["Commands\Read.exe", ARG2], shell=True)
However I still get the console appear every time an external command is called

There might be two issues here. First off, if your python scripts have the .pyw extension then they will be associated with pythonw which does not use a console*. However, you have shell=True, which generates a console*. You need to run the program and hide the console:
import subprocess
proc = subprocess.Popen('hello.py', creationflags=subprocess.SW_HIDE, shell=True)
proc.wait()
*Pedantically, it's not a dos prompt, it is a console window. DOS - Disk Operating System - was an IBM mainframe OS. MS-DOS or PC-DOS command-line features were mirrored (with a lot of extra features) by cmd.exe (a Windows shell), which is a console program and so uses a console window. It's that console window you need to hide.

You need to use startupinfo parameter of subprocess.Popen() class' constructor.
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen(command, startupinfo=startupinfo)
You do not need shell=True if all you want is to hide console window; see this answer.

Related

Unable to hide the command prompt open by GUI exe [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running a process in pythonw with Popen without a console
How do I eliminate Windows consoles from spawned processes in Python (2.7)?
I have a Python program that calls a separate number-crunching program (written in C) as a subprocess several times (using subprocess.check_call). It works great on Linux, and it works great on Windows too except for one little thing: every time it calls the subprocess a Command Prompt window is created and then soon destroyed when the subprocess exits.
This doesn't affect the computation at all, but it's very annoying because this window keeps flashing on and off the screen, and it makes it difficult to do other things on the computer because the new Command Prompt window can steal keyboard focus.
How can I simply execute the subprocess (which has no GUI), and prevent the Command Prompt window from being created?
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)
When you build the C application, set its type to the Win32 Subsystem instead of the Console Subsystem. If this is a pre-built application, you could change the subsystem with this tool.

Issue terminal commands that are piped to a shell script

I have what seems to be a simple use case: I launch a script (python or bash) which runs an emulator from command prompt and then the emulator takes commands until I type ctrl-c or exit. I want to do this same thing from a shell and my code below isn't working. What I am trying to do is test automation so I want to issue commands directly to the application from command shell. In python, I have the following:
import os
import subprocess
command = ['/usr/local/bin/YCTV-SIM.sh', '-Latest'] #emulator for yahoo widgets
process = subprocess.Popen( command, shell=True, stdin=subprocess.PIPE )
time.sleep(12) #wait for launch to finish
print '/widgets 1' #first command to issue
print '/key enter' #second command to issue
process.wait()
As you can see, this is some pretty simple stuff. When 'YCTV-SIM.sh' is launched from the command shell, I am put into an input mode and my key entries are sent to the application shell (YCTV-SIM.sh reads raw input) so ideally, I would be able to pipe text directly to this application shell. So far tho, nothing happens; test outputs to the console window but the application does not respond to the commands that I attempt to issue. I am using python 2.6.3, if that matters, but Python is not required..
Language is immaterial at this point so PERL, Python, Bash, TCL... whatever you can suggest that might help.
You need to redirect stdin of the child process and write into it. See e.g. subprocess.Popen.communicate.

Python - Stray cmd.exe window causes program to hang. How can I close this stray program?

I have a Python file which starts a new process via Popen. The Python codes should wait until this new process is complete before resuming it's code. However, the process Python starts also spawns another cmd.exe window which does some work, but stays open after it's done. The process Python starts does close after it's done, but the stay cmd.exe window remains open causing my Python program not to continue. What are the best ways to close this stay window?
Here's the call I'm making to popen:
p = Popen(command, stdout=PIPE, stderr=STDOUT, stdin=PIPE)
command is running another python file. I set it equal to p because I later communicate with the new process via grep_stdout.
This can be solved by using pythonw.exe instead of python.exe to execute your second script. If you're running the script directly (rather than invoking the interpreter yourself), you should rename it with a .pyw file extension, as that should be associated with pythonw.exe by default.
See this section of the Python docs for more information about executing Python files on Windows.

Running a commandline application from GUI silently

I would like to run the specific commandline application:
ffmpeg -i video.mp4 audio.mp3
I'm running the command through a GUI, and when the console window doesn't exist, the ffmpeg process is running in a new cmd window.
Testers find the "black window that appears" scary and not userfriendly.
How can I run the application without any visible window coming up? os.system(), subprocess.Popen() and subprocess.call() all do launch the cmd window.
If it matters, I'm using pyqt4 and py2exe. I'm targeting Windows OS users.
This recipe at ActiveState may solve your problem:
http://code.activestate.com/recipes/409002/
Slight changes are required for Python 2.7. See How do I eliminate Windows consoles from spawned processes in Python (2.7)?
Launch ffmpeg from the START command. If you use the /B switch, no command window will be shown.
Use subprocess.Popen (or call) and redirect stdout/stderr somewhere. They're currently hooked to your own process's stdout and stderr, which is why they're coming through.
If you need something that can integrate nicely with your GUI event loop, use Twisted's process-launching stuff.

Executing subprocess from Python without opening Windows Command Prompt [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running a process in pythonw with Popen without a console
How do I eliminate Windows consoles from spawned processes in Python (2.7)?
I have a Python program that calls a separate number-crunching program (written in C) as a subprocess several times (using subprocess.check_call). It works great on Linux, and it works great on Windows too except for one little thing: every time it calls the subprocess a Command Prompt window is created and then soon destroyed when the subprocess exits.
This doesn't affect the computation at all, but it's very annoying because this window keeps flashing on and off the screen, and it makes it difficult to do other things on the computer because the new Command Prompt window can steal keyboard focus.
How can I simply execute the subprocess (which has no GUI), and prevent the Command Prompt window from being created?
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)
When you build the C application, set its type to the Win32 Subsystem instead of the Console Subsystem. If this is a pre-built application, you could change the subsystem with this tool.

Categories