Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PythonW does not run script. Seriously

Tags:

python

So, using Windows 10 and Python 3.6. I've created a .py script that runs fine in command prompt using the command python myscript.py, but when I make an exact copy of that script and give it the extension .pyw, and try to run it with pythonw with the command pythonw myscript.pyw, nothing happens. pythonw does not show up in my task manager at all, and part of this script sends feedback to my slack channel so I would know if it's "running in the background" or not. I did see pythonw flash in my task manager for half a second before disappearing, so this tells me that the script is somehow crashing, but why would this be if it runs with python just fine? I want to run this script as a background process and not with the python command.

I've tried adding:

sys.stdout = open('log.txt', 'w')
sys.stderr = open('err.txt', 'w')

to the top of the .pyw script to force the outputs into those files. I've tried running the command pythonw myscript.py, still nothing, and have also tried just setting the pythonw.exe as the main program to open .pyw files, so I double click the script to run it, nothing starts. I know it runs in the background without a command prompt, that's what I want, but the program itself isn't running. I've check my environment variable paths as well which isn't the issue as pythonw.exe is in the same folder as python.exe and python.exe runs from command prompt without issue.

What else can I try?

like image 906
Matt Wilson Avatar asked Jun 28 '26 16:06

Matt Wilson


1 Answers

For scripts that work right with python.exe but not with pythonw.exe, this is usually due to the fact that stdout and stderr don't exist when you execute a script with pythonw (or pyw).

So, if you have any print commands, they will try to stream.write(... and python internally complaints:AttributeError: 'NoneType' object has no attribute 'write', since pythonw has no console to write to.

You can check this with this script, that writes output to a file, as @wildcat89 suggested:

try:
    print("Hello")
except:
    import traceback
    with open("trypyw.out.txt",'w') as a:
        a.write(traceback.format_exc())
else:
    with open("trypyw.out.txt",'w') as a:
        a.write("OK")

If this is the case, I have used this solution:

# The first lines of your scrip should be these (files will be closed on exit)
import sys
sys.stdout = open('stdout.txt', 'w')
sys.stderr = open('stderr.txt', 'w')

If you want only to redirect it if executed with pythonw from a direct access (not from console):

# The first lines of your scrip should be these (files will be closed on exit)
import os
if not 'PROMPT' in os.environ:
    import sys
    sys.stdout = open('stdout.txt', 'w')
    sys.stderr = open('stderr.txt', 'w')

your code
...

If no output is desired:

import os
if not 'PROMPT' in os.environ:
    import sys
    nullfile = open(os.devnull, 'w')
    sys.stdout = nullfile
    sys.stderr = nullfile

your code
...

Other solutions could be some of the responses to this question: Redirect stdout to a file in Python?

like image 106
Anr Avatar answered Jun 30 '26 07:06

Anr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!