0

I was hoping to freeze a Python CLI made with click using PyInstaller.

I found this answer which makes it work with click commands, unfortunately it doesn't work for applications with subcommands.

I've got the following code to demonstrate the issue:

In cli.py

import sys

import click


@click.group()
def bar():
    pass


@bar.command()
def baz():
    click.echo('yo')


if __name__ == "__main__":
    if getattr(sys, 'frozen', False):
        bar(sys.argv[1:])
    else:
        bar()

I run the following pyinstaller command: pyinstaller --noconfirm --name bar --clean "cli.py"

And I get the following output:

Traceback (most recent call last):
  File "tests\bar\cli.py", line 18, in <module>
  File "click\core.py", line 722, in __call__
  File "click\core.py", line 696, in main
  File "click\core.py", line 621, in make_context
  File "click\core.py", line 1015, in parse_args
  File "click\utils.py", line 259, in echo
  File "click\_compat.py", line 577, in _safe_write
  File "colorama\ansitowin32.py", line 40, in write
  File "colorama\ansitowin32.py", line 141, in write
  File "colorama\ansitowin32.py", line 169, in write_and_convert
  File "colorama\ansitowin32.py", line 174, in write_plain_text
  File "click\_winconsole.py", line 180, in write
  File "click\_compat.py", line 63, in write
  File "click\_winconsole.py", line 164, in write
OSError: Windows error 6
[173704] Failed to execute script cli

I am using Python 2.7.13 on a Windows 10 OS.

I could not try py2exe, cx-Freeze, briefcase or the likes due to my job environment.

Has someone else stumbled upon this issue before ?

I'll try with argparse to see if I got the same issue.

Aphosis
  • 19
  • 1
  • 4
  • It is trying to print the help. Windows error 6 is *file handle invalid*. How old (what version) is your colorama? – Stephen Rauch Oct 15 '19 at 14:02
  • Thanks for your interest. I was on colorama `0.3.9`. I tried upgrading to `0.4.1`, but still have the same error. I also tried with `argparse`, and there was no error. I suppose it comes from the fact `argparse` does no color handling ? – Aphosis Oct 16 '19 at 09:10
  • Yes, with that stack trace, seems likely to be an incompatibly in the color handling in Windows 10 when frozen. I would also suspect (investigate) the very soon to be obsolete version of Python maybe causing some problems. – Stephen Rauch Oct 16 '19 at 11:57
  • Sorry for the late response, I've done some more testing since. I found [this issue](https://github.com/pyinstaller/pyinstaller/issues/3178) which details some workarounds, unfortunately none worked for me, I'll use argparse for the time being. Thanks for your time ! – Aphosis Oct 17 '19 at 10:41

1 Answers1

1

So, after some more invstigation, it seems that Windows 10, Python 2.7.x and kernel32.WriteConsoleW don't bode well for pyinstaller.

Specifically, I found a similar issue on pyinstaller's github, in which some people detail the behavior of a Windows feature known as High Entropy for Bottom-up ASLR.

So there is 3 solutions or workarounds suggested in this link:

  • Install, import and enable() win_unicode_console.
  • Disable High Entropy on the generated executable properties.
  • Upgrade to Python 3.

Check the link for more details, it's all black magic to me.

Unfortunately, I cannot upgrade to Python 3, couldn't find the high entropy setting, and the win_unicode_console trick did not work, so I'll stick with argparse.

The best solution would be to upgrade to Python 3 if possible.

Thanks Stephen for your help !

Aphosis
  • 19
  • 1
  • 4