Running a simple .py or .pyw python file causes python.exe
to show up under Task Manager.
python myApp.py
python myApp.pyw
However when we try to run it without using the console, the script does not appear to run, nor does python.exe
or pythonw.exe
appears under Task Manager
pythonw myApp.pyw
pythonw myApp.py
How do we troubleshoot the problem? The system is running Python 2.7.8 x64.
I faced the same problem on a script of my own and found that when adding the output from Ross' answer the script would actually run.
It appears that for some reason that redirecting output fixes the problem. Since I'm not interested in writing the output to disk I've instead written it to
/dev/null
(or the platform equivalent) with:The if statement ensures it only happens when the script is launched from
pythonw.exe
. I'm not sure if it is related but it was important to do this before other imports (including e.g.import logging
).I was having similar problem.
After debugging step by step by writing to a log file, I discovered that pythonw.exe crashed after a statement that tried to use the call: sys.stdout.write(). It turns out, when run with pythonw.exe, sys.stdout is None.
If you are using functions of sys.stdout/stderr/stdin, and intend to use your program with pythonw.exe, adding a check for "None" is a good idea.
Im not sure I understand your problem but I think this is what you need to know
you need to right click on a py or pyw file and select open with ... find python.exe (probably C:\Python27\python.exe) .. check the box that says always open ... now you can just double click it if you want to run it
(usually the installer sets this up for you ...)
I had a similar problem after an upgrade to my computer RAM. Turns out I had to reinstall Pillow (library used for image processing). So make sure it is installed and if it's not, install it using "pip install Pillow" in cmd.
Try adding the line
import sys; sys.stderr = open("errlog.txt", "w")
to the start ofmyApp.py
. Then look inerrlog.txt
for a traceback or any other error messages.tl;dr
To troubleshoot, use output redirection on invocation:
This will capture stdout output, such as from
print()
, in filestdout.txt
, and stderr output (such as from unhandled exceptions), in filestderr.txt
; from PowerShell, usecmd /c pythonw myApp.py 1>stdout.txt 2>stderr.txt
).Note that the very act of redirecting stdout may actually make your script work again, if the only reason for its failure with
pythonw
was the use ofprint
(in Python 2.x - see below).Caveat: This output redirection technique seemingly does not work when invoking
*.pyw
scripts directly (as opposed to by passing the script file path topythonw.exe
). Do let me know if you know why and/or if it does work for you.Place the following at the top of any Python 2.x or 3.x script that you want to run with
pythonw.exe
:This ensures the following when a script is run with
pythonw.exe
:print()
calls and explicit calls tosys.stdout()
are effectively ignored (are no-ops).%TEMP%\stderr-<scriptFileName>
;%TEMP%
is a standard Windows environment variable that points to the current user's folder for temporary files.In other words: With the above code in place, check file
%TEMP%\stderr-<scriptFileName>
after your script has failed silently when invoked withpythonw.exe
.For an explanation, read on.
On Windows,
pythonw.exe
is for launching GUI/no-UI-at-all scripts, which means that the standard in- and output streams -sys.stdin
,sys.stdout
,sys.stderr
are NOT available.This has two nasty side effects:
print()
- which targetssys.stdout
by default - causes an exception in Python 2.x.print()
in 2.x - causes the script to abort silently.sys.stderr
by default, which is the very thing not available in this scenario.The above code fixes these problems by:
sending stdout output to the null device, effectively ignoring any attempt to output to
sys.stdout
- whether explicitly, or implicitly viaprint()
.sending all stderr output to a temporary file.
Differences between Python 2.x and Python 3.x:
When a script is run with
pythonw.exe
,sys.stdin
,sys.stdout
, andsys.stderr
:sys.stdout
orsys.stderr
is the following exception:IOError: [Errno 9] Bad file descriptor
pythonw.exe
with-u
(for unbuffered output).print()
blindly tries tosys.stdout
(by default), so it provokes this exception sooner or later.None
print()
function performing a no-op (doing nothing) when it finds thatsys.stdout
isNone
, so thatprint()
statements can by default safely be used - they'll simply be ignored when run withpythonw.exe
sys.stdout.write()
andsys.stderr.write()
still results in an exception.See here for more background.