Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyinstaller is not compatible with python argparse -h

In the python script, ArgumentParser will displays the parser’s help message by default if -h or --help is supplied at the command line.

(base) D:\Study\Github\BVPA-GUI>python src\plot_auto.py -h                         
usage: plot_auto.py [-h] [--legend] [--figname FIGNAME]

optional arguments:
  -h, --help         show this help message and exit
  --legend           Show legend on plot.
  --figname FIGNAME  Specify the window title.

However it will not work if I compile the python script into exe application by pyinstaller.

D:\Study\Github\BVPA-GUI>.\bin\plotting\plot_auto.exe -h

What is the reason for this and what can I do about this?

like image 930
Mystery Davil Avatar asked Aug 26 '26 02:08

Mystery Davil


1 Answers

Have you ever used sys.argv with pyinstaller? You can check that it works well.

See argparse python official document, especially parse_args parts. It makes a parsed arguments with the given console arguments or the given string list on function interface, parse_args(['arg1', 'arg2', ... ]).

Let's apply to your application. Very simple.

import sys, argparse

parser = argparse.ArgumentParser()

# add arguments settings ------------
 .
 .
 .
#------------------------------------

if __name__ == "__main__":
    args = parser.parse_args(sys.argv[1:])
    # Your program code

I also tried same work with you.

It worked well to me.

I hope it is usefull to you too.

#Note: In the official document said that parse_args function get default arguments from sys.argv but it doesn't seems to work with pyinstaller. The above is just manually add such progress.

like image 85
HornPenguin Avatar answered Aug 28 '26 16:08

HornPenguin