If I save my code files as .pyw
, no console window appears - which is what I want - but if the code includes a call to os.system
, I still get a pesky console window. I assume it's caused by the call to os.system
. Is there a way to execute other files from within my .pyw
script without raising the console window at all?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
People are a bit lazy... I would thx @Piotr Dobrogost and @Frank S. Thomas for their answers.
I came with this code who is runinng on Linux and Windows:
Later...
Thx guys ;)
Additionally: just to note that the following code using 'call' also works on Python 2.7 (on Windows) with the 'startupinfo' code above:
Similar to what @firsthand said, I've read on the wxPython-user forums that you "replace" the current running application, that would be "command.com" or "CMD.exe", with pyw.exe or pythonw.exe when you use something like the following:
see another post
Although I do not know how you would pipe io in this case.
I believe one benefit of this approach is if you run your script multiple times your OS taskbar with not fill up with CMD icons. The other way if you have several CMD minimized in the taskbar and start closing them, it is impossible to tell which CMD goes with which pythonw script.
It seems that 'os.popen' doesn't produce console window. Script is running under 'pythonw'. Not sure about all cases but in my case it works well.
The solution that Piotr describes is actually not as complicated as it may sound. Here is an example where a
startupinfo
is passed to acheck_call
invocation to suppress the console window:Since the convenience functions
call
,check_call
, andcheck_output
forward their**kwargs
to thePopen
constructor, it is not required to usePopen
directly.You could try using the subprocess module (
subprocess.Popen
,subprocess.call
or whatever) with the argumentshell=True
if you want to avoid starting a console window.You should use
subprocess.Popen
class passing asstartupinfo
parameter's value instance ofsubprocess.STARTUPINFO
class withdwFlags
attribute holdingsubprocess.STARTF_USESHOWWINDOW
flag andwShowWindow
attribute holdingsubprocess.SW_HIDE
flag. This can be inferred from reading lines 866-868 ofsubprocess.py
source code. It might be necessary to also passsubprocess.CREATE_NEW_CONSOLE
flag as a value ofcreationflags
parameter as you run underpythonw.exe
which does not open a console.When you use
shell=True
it just happens that all of the above is set correctly but that doesn't mean it's a proper solution. I would argue it's not because it adds overhead of running command interpreter and parsing arguments. In addition you should keep in mind that (...) the use of shell=True is strongly discouraged in cases where the command string is constructed from external input according to documentation of subprocess module.