1

EDIT: Please note, I have seen other topics concerning this issue and already tried most of the suggestions there

I use pyinstaller to run an .exe file and now I am tryng to run it without a console (using the -w command). One of my key libraries, patool, uses subprocesses, which gives the following error:

Traceback (most recent call last):
  File "apscheduler\executors\base.py", line 125, in run_job
  File "script.py", line 478, in Archiver
  File "patoolib\__init__.py", line 521, in _create_archive
  File "patoolib\__init__.py", line 421, in run_archive_cmdlist
  File "patoolib\util.py", line 227, in run_checked
  File "patoolib\util.py", line 219, in run
  File "subprocess.py", line 339, in call
  File "subprocess.py", line 753, in __init__
  File "subprocess.py", line 1090, in _get_handles
OSError: [WinError 6] The handle is invalid

Here is the part of the patool util.py code which has the subprocesses.call() that gives the error:

def run (cmd, verbosity=0, **kwargs):
    """Run command without error checking.
    @return: command return code"""
    # Note that shell_quote_nt() result is not suitable for copy-paste
    # (especially on Unix systems), but it looks nicer than shell_quote().
    if verbosity >= 0:
        log_info("running %s" % " ".join(map(shell_quote_nt, cmd)))
    if kwargs:
        if verbosity >= 0:
            log_info("    with %s" % ", ".join("%s=%s" % (k, shell_quote(str(v)))\
                                           for k, v in kwargs.items()))
        if kwargs.get("shell"):
            # for shell calls the command must be a string
            cmd = " ".join(cmd)
    if verbosity < 1:
        # hide command output on stdout
        with open(os.devnull, 'wb') as devnull:
            kwargs['stdout'] = devnull
            res = subprocess.call(cmd, **kwargs)       <------------- ERROR 
    else:
        res = subprocess.call(cmd, **kwargs)
    return res

This is a common error, so I tried reading about subprocesses module and also dug out every single possible suggestion online, including:

Neither of these works, the handler is still invalid. The programme works fine with the console active. I am using Python 3.7, Anaconda3, 64-bit Windows 10 OS.

Later in the util.py there is a subprocess.popen() that I suspect will cause my the same problem. I was trying to run the .exe by having the console active and then hiding it, but then I encounter other problems (it does not start upon system start up). I guess the console being present is pretty important, but I would love to get rid off it for better user experience.

ISquared
  • 364
  • 4
  • 22
  • Have you tried with `kwargs['stdout'] = subprocess.DEVNULL`? (Not very optimistic; the documentation implies that it's simply an alias for `os.devnull`.) – tripleee Aug 09 '20 at 10:31
  • The linked question talks about **`stdin`** whereas you are manipulating `stdout`. Try connecting both to `os.devnull`. If that solves it, this is basically a duplicate of https://stackoverflow.com/questions/40108816/python-running-as-windows-service-oserror-winerror-6-the-handle-is-invalid – tripleee Aug 09 '20 at 10:42
  • Thank you for the prompt reply! Why do you say I am manipulating stdout? The code I posted is just the original code as it comes from patool util.py - the stdout manipulation is there in the original code. Just below that, I say I also add `kwargs['stdin'] = devnull` to this original code. I have also tried adding `stdin=subprocess.DEVNULL` as an argument to the call() method, although it should be the same as doing kwargs['stdin'] = devnull anyway. – ISquared Aug 09 '20 at 11:01
  • I tried implementing `kwargs['stdout'] = subprocess.DEVNULL` and `kwargs['stdin'] = subprocess.DEVNULL` - no change :( – ISquared Aug 09 '20 at 11:05

1 Answers1

0

Fixed it by adding: stdin=subprocess.PIPE in this file for python 3.8: C:\Program Files (x86)\Python38-32\Lib*os.py* line 983 to 991

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, **stdin=subprocess.PIPE**, stderr=subprocess.STDOUT,bufsize=buffering)