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.
Related
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.
I want to write a Python script which will start a GUI program (as in, run a binary program with subprocess.run or os.system or something). The script should not block until the program is done, it should start it and then keep running.
I'm doing this on Ubuntu.
I tried subprocess.Popen, but if I run, say subprocess.Popen("gedit"), I get strange behavior. If I open the Ubuntu system monitor (process manager), I can see that the gedit process appears when I run the script, and the gedit window itself opens. But if I close the window, the process doesn't end in the system monitor. The process stays there until my python script exits.
How can I get the behavior I want? The only thing I can think of right now is to just call subprocess.run in a different Python thread, is that the only thing I can do?
Try using subprocess.call. This has worked for me before.
subprocess.call(['command', 'arguments'])
The program should end when the window is closed.
You have to kill the subprocess you've created before you exit the program.
Try this.
I am trying to launch a program/GUI from within a python code.
From the terminal, I can get the program to launch by simply typing the program name. A few lines get outputted to the terminal, and then a separate window opens with the GUI.
I tried to emulate this in python by running
os.system("<program name>")
The typical output lines, as mentioned above, get printed to the console, but no window opens up with the GUI.
Can os.system() be used to execute programs that have their own separate window?
From the Python manual:
[os.system] is implemented by calling the Standard C function
system()
That being said, you shouldn't have any problems launching a GUI application with os.system. I've just tried it myself and it works fine.
It also mentions in the manual that:
The subprocess module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is
preferable to using this function.
Maybe that's worth a try. Do any other GUI applications work when you spawn them with os.system?
Here is a solution using subprocess
import subprocess
subprocess.Popen("notepad.exe")
Or if you want to run a python program with a specific interpreter:
subprocess.Popen('{0} {1}'.format(PythonInterpreterPath,PythonFilePath.py))
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.
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.