1

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?

wildcat89
  • 1,159
  • 16
  • 47
  • 1
    I would try to determine if it's something in the script that pythonw just doesn't like (yet python runs) - using the redirection of `stdout` and `stderr`, try a *much simpler script* - a "Hello, World", basically - to see if pythonw can launch *any* script on your system. If so, then something is with the script - an ImportError of some sort, most likely. If even a "Hello, World" can't run, then something is with the installation/configuration of pytohnw. – CoolBots Oct 17 '20 at 23:05
  • Thanks! Didn’t know imports could be the issue. I’ll start stripping some things out until I pinpoint which import (or function) is causing the issue. – wildcat89 Oct 17 '20 at 23:19
  • 2
    You could wrap the entire outer script - including the imports at the top - in a try/except and write the exception to a hard coded file name. Alternately, you could `atexit.register()` an exit handler that prints out the information from `sys.exc_inf()` . Again, do this before other imports. Then we'd have more info to work with. – tdelaney Oct 17 '20 at 23:20
  • Oh wicked. Thanks! I know it’s a broad question and there’s a lot of imports that could be messing with it. I’ll try these suggestions later tonight and report what I find out – wildcat89 Oct 17 '20 at 23:39
  • I found the issue with your help! It was definitely an import issue with one of my dependencies, which is odd because it works with the python.exe command, just not pythonw for some reason. So I'm going to address uninstalling/reinstalling/directing the proper filepath for the dependency to fix it. Basically I just commented the entire script out, add those two err and std outputs to the top, and then one at a time uncommented each dependency and reviewed the errors until I found the one causing the issue. Thanks!!!!! – wildcat89 Oct 18 '20 at 19:29

1 Answers1

0

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?

Anr
  • 332
  • 3
  • 6