Python running as Windows Service: OSError: [WinError 6] The handle is invalid
Line 1117 in subprocess.py
is:
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
which made me suspect that service processes do not have a STDIN associated with them (TBC)This troublesome code can be avoided by supplying a file or null device as the stdin argument to popen
.
In Python 3.x, you can simply pass stdin=subprocess.DEVNULL
. E.g.
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
In Python 2.x, you need to get a filehandler to null, then pass that to popen:devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
pyinstaller error: OSError: [WinError 6] The handle is invalid
This error apparently is thrown, because of this:
Line 1117 in subprocess.py is:This problem can be avoided by supplying a file or null device as the stdin argument top2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
The service processes do not have a STDIN associated with them (TBC).
popen
.In Python 3.x, you can simply passReference: OSError: (WinError 6) The handle is Invalidstdin=subprocess.DEVNULL
.
E.g.In Python 2.x, you need to get a filehandler to null, then passsubprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
that to popen:devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
In your problem:
subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True, stdin=subprocess.DEVNULL).stdout.decode()
WinError 6: The handle is invalid from python check_output spawn in Electron app
On Windows, subprocess.Popen
tries to duplicate non-zero standard handles and fails if they're invalid. You can redirect stdin
and stderr
to NUL. For example:
import os
try:
from subprocess import DEVNULL
except ImportError:
DEVNULL = os.open(os.devnull, os.O_RDWR)
output = subprocess.check_output(cmd, stdin=DEVNULL, stderr=DEVNULL)
WinError 6 - The handle is invalid with executable but code works on debug mode
The problem is with psa1_path
variable
C:/Users/${USERNAME}\projects\demo\cmd\powershell.ps1 -m -v 'CC 2018' -wait windowstyle hidden
This variable has parameters. And the subprocess.Popen
use it as a string, then you have to set shell=True
so Popen will use this string as a complete shell cmd.p = subprocess.Popen(['C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
psa1_path],
stdout=f, shell=True, stdin=subprocess.DEVNULL)
Don't forget to add stdin arg because it's also throw the [WinError 6] The handle is invalid
So why the code works with debug code and not with executable :
It's mainly because PyCharm does additionnal configuration and setup behind the scenes when it comes to running a program.
Because when you’re going from IDE to runtime, you need additional hooks to get things going. pyinstaller doesn’t do the subprocess the same way as pyCharm for the shell part
Related Topics
Python and Operator on Two Boolean Lists - How
How to Convert an Integer to the Shortest Url-Safe String in Python
Dump to JSON Adds Additional Double Quotes and Escaping of Quotes
How to Overlay Two Graphs in Seaborn
Loading Initial Data with Django 1.7 and Data Migrations
Pycharm: Set Environment Variable for Run Manage.Py Task
How to Get All the Request Headers in Django
Getting Processor Information in Python
How to Plot Empirical Cdf (Ecdf)
Get the String Within Brackets in Python
Strip/Trim All Strings of a Dataframe
Slicing a List into N Nearly-Equal-Length Partitions
Plotting Multiple Lines, in Different Colors, with Pandas Dataframe
How to Group a Pandas Dataframe by a Defined Time Interval
How to Extract a Subset of a Colormap as a New Colormap in Matplotlib