2

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?

  • Does [this](https://stackoverflow.com/questions/39577267/how-to-make-argparse-work-in-executable-program) answer your question? – luizbarcelos Sep 01 '21 at 03:17
  • 1
    thanks @luizbarcelos but this didnt help much. I was looking for an explanation and the answer did not address the issue. – Mystery Davil Sep 01 '21 at 03:21
  • Did you ever find a solution for this? I am having the same problem and this question was the only thing I could find about it. – Andrew Jan 05 '22 at 15:57
  • I tried this, too, to add a "help" argument, but it doesn't work either. https://stackoverflow.com/questions/35847084/customize-argparse-help-message – Andrew Jan 05 '22 at 16:36
  • I think this is a serious issue. I could not even catch the exception to allow for a simple printed explanation before exiting. It is reasonable to expect the same behavior from argparse within PyInstaller. If that is not the case, then I think a coherent reason is warranted. I think it has something to do with different initialization procedures between normal Python and PyInstaller. Something in argparse acts differently. – Jiminion May 17 '23 at 13:58
  • Started a bounty. – Jiminion May 18 '23 at 14:06
  • @Jiminion I know `optparse` is deprecated but seems like it works with pyinstaller when I tested with `-h` and `--help` on python 3.9. – Ali Ent May 18 '23 at 15:11
  • @AliEnt I seem to have a version that sort of works now. Not sure what is different. It was built along the lines of Horn Penguin's answer. Still looking into it. – Jiminion May 18 '23 at 17:14
  • I can't reproduce. Using *Python 3.10*, *PyInstaller 5.90.* on *Win* works OK. – CristiFati May 20 '23 at 21:42
  • @CristiFati Did you have to catch the error in argparse with a try? – Jiminion May 22 '23 at 14:22
  • @Jiminion: No, didn't have to do anything. It just works. – CristiFati May 22 '23 at 14:35

3 Answers3

1

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.

0

I might be missing something, but I can't reproduce.

code00.py:

#!/usr/bin/env python

import argparse
import sys


def parse_args(*argv):
    parser = argparse.ArgumentParser(description="q069007351 demo")
    parser.add_argument("--dummy", "-d", default=2.718282, help="Dummy argument")
    args, unk = parser.parse_known_args()
    if unk:
        print("Warning: Ignoring unknown arguments: {:}".format(unk))
    print("Args: {:}".format(args))
    return args.dummy


def main(*argv):
    pa = parse_args()
    print("parse_args returned: {:}".format(pa))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

Output:

  • Win:

    [cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q069007351]> sopr.bat
    ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
    
    [prompt]>
    [prompt]> dir /b
    code00.py
    
    [prompt]>
    [prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\pyinstaller.exe" -v
    5.9.0
    
    [prompt]>
    [prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\pyinstaller.exe" -F code00.py
    828 INFO: PyInstaller: 5.9.0
    828 INFO: Python: 3.10.9
    848 INFO: Platform: Windows-10-10.0.19045-SP0
    851 INFO: wrote e:\Work\Dev\StackExchange\StackOverflow\q069007351\code00.spec
    855 INFO: UPX is not available.
    865 INFO: Extending PYTHONPATH with paths
    ['e:\\Work\\Dev\\StackExchange\\StackOverflow\\q069007351']
    pygame 2.1.2 (SDL 2.0.18, Python 3.10.9)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    4278 INFO: checking Analysis
    4278 INFO: Building Analysis because Analysis-00.toc is non existent
    4281 INFO: Initializing module dependency graph...
    4289 INFO: Caching module graph hooks...
    4324 INFO: Analyzing base_library.zip ...
    8266 INFO: Loading module hook 'hook-heapq.py' from 'E:\\Work\\Dev\\VEnvs\\py_pc064_03.10_test0\\lib\\site-packages\\PyInstaller\\hooks'...
    8509 INFO: Loading module hook 'hook-encodings.py' from 'E:\\Work\\Dev\\VEnvs\\py_pc064_03.10_test0\\lib\\site-packages\\PyInstaller\\hooks'...
    11833 INFO: Loading module hook 'hook-pickle.py' from 'E:\\Work\\Dev\\VEnvs\\py_pc064_03.10_test0\\lib\\site-packages\\PyInstaller\\hooks'...
    15534 INFO: Caching module dependency graph...
    15720 INFO: running Analysis Analysis-00.toc
    15749 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
      required by c:\Install\pc064\Python\Python\03.10\python.exe
    15825 INFO: Analyzing e:\Work\Dev\StackExchange\StackOverflow\q069007351\code00.py
    15836 INFO: Processing module hooks...
    15854 INFO: Looking for ctypes DLLs
    15857 INFO: Analyzing run-time hooks ...
    15859 INFO: Including run-time hook 'E:\\Work\\Dev\\VEnvs\\py_pc064_03.10_test0\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
    15874 INFO: Looking for dynamic libraries
    817 INFO: Extra DLL search directories (AddDllDirectory): []
    818 INFO: Extra DLL search directories (PATH): ['C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\WINDOWS\\System32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\Install\\pc064\\Docker\\Docker\\Version\\Docker\\resources\\bin', 'C:\\Program Files\\dotnet\\', 'e:\\Work\\Dev\\Utils\\current\\Win', 'e:\\Work\\Dev\\VEnvs\\py_pc064_03.10_test0\\Scripts', 'C:\\Users\\cfati\\.dotnet\\tools']
    17695 INFO: Looking for eggs
    17695 INFO: Using Python library c:\Install\pc064\Python\Python\03.10\python310.dll
    17700 INFO: Found binding redirects:
    []
    17703 INFO: Warnings written to e:\Work\Dev\StackExchange\StackOverflow\q069007351\build\code00\warn-code00.txt
    17736 INFO: Graph cross-reference written to e:\Work\Dev\StackExchange\StackOverflow\q069007351\build\code00\xref-code00.html
    17807 INFO: checking PYZ
    17808 INFO: Building PYZ because PYZ-00.toc is non existent
    17810 INFO: Building PYZ (ZlibArchive) e:\Work\Dev\StackExchange\StackOverflow\q069007351\build\code00\PYZ-00.pyz
    18213 INFO: Building PYZ (ZlibArchive) e:\Work\Dev\StackExchange\StackOverflow\q069007351\build\code00\PYZ-00.pyz completed successfully.
    18229 INFO: checking PKG
    18229 INFO: Building PKG because PKG-00.toc is non existent
    18230 INFO: Building PKG (CArchive) code00.pkg
    20220 INFO: Building PKG (CArchive) code00.pkg completed successfully.
    20223 INFO: Bootloader E:\Work\Dev\VEnvs\py_pc064_03.10_test0\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
    20223 INFO: checking EXE
    20225 INFO: Building EXE because EXE-00.toc is non existent
    20225 INFO: Building EXE from EXE-00.toc
    20226 INFO: Copying bootloader EXE to e:\Work\Dev\StackExchange\StackOverflow\q069007351\dist\code00.exe.notanexecutable
    20297 INFO: Copying icon to EXE
    20304 INFO: Copying icons from ['E:\\Work\\Dev\\VEnvs\\py_pc064_03.10_test0\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
    20339 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
    20340 INFO: Writing RT_ICON 1 resource with 3752 bytes
    20341 INFO: Writing RT_ICON 2 resource with 2216 bytes
    20344 INFO: Writing RT_ICON 3 resource with 1384 bytes
    20344 INFO: Writing RT_ICON 4 resource with 37019 bytes
    20345 INFO: Writing RT_ICON 5 resource with 9640 bytes
    20345 INFO: Writing RT_ICON 6 resource with 4264 bytes
    20346 INFO: Writing RT_ICON 7 resource with 1128 bytes
    20349 INFO: Copying 0 resources to EXE
    20349 INFO: Embedding manifest in EXE
    20353 INFO: Updating manifest in e:\Work\Dev\StackExchange\StackOverflow\q069007351\dist\code00.exe.notanexecutable
    20393 INFO: Updating resource type 24 name 1 language 0
    20396 INFO: Appending PKG archive to EXE
    20408 INFO: Fixing EXE headers
    21489 INFO: Building EXE from EXE-00.toc completed successfully.
    
    [prompt]>
    [prompt]> dist\code00.exe --help
    Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32
    
    usage: code00.exe [-h] [--dummy DUMMY]
    
    q069007351 demo
    
    options:
      -h, --help            show this help message and exit
      --dummy DUMMY, -d DUMMY
                            Dummy argument
    
    [prompt]>
    [prompt]> dist\code00.exe -x
    Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32
    
    Warning: Ignoring unknown arguments: ['-x']
    Args: Namespace(dummy=2.718282)
    parse_args returned: 2.718282
    
    Done.
    
  • Nix:

    (py_pc064_03.09_test0) [cfati@cfati-5510-0:/mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351]> ~/sopr.sh
    ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
    
    [064bit prompt]>
    [064bit prompt]> ls
    build  code00.py  code00.spec  dist
    [064bit prompt]>
    [064bit prompt]> pyinstaller -v
    4.7
    [064bit prompt]>
    [064bit prompt]> pyinstaller -F code00.py
    75 INFO: PyInstaller: 4.7
    75 INFO: Python: 3.9.16
    79 INFO: Platform: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.31
    83 INFO: wrote /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/code00.spec
    113 INFO: UPX is not available.
    128 INFO: Extending PYTHONPATH with paths
    ['/mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351']
    379 INFO: checking Analysis
    404 INFO: Building because inputs changed
    404 INFO: Initializing module dependency graph...
    413 INFO: Caching module graph hooks...
    419 INFO: Analyzing base_library.zip ...
    2433 INFO: Processing pre-find module path hook distutils from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'.
    2436 INFO: distutils: retargeting to non-venv dir '/usr/lib/python3.9'
    4922 INFO: Caching module dependency graph...
    5196 INFO: running Analysis Analysis-00.toc
    5249 INFO: Analyzing /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/code00.py
    5266 INFO: Processing module hooks...
    5267 INFO: Loading module hook 'hook-heapq.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5269 INFO: Loading module hook 'hook-pickle.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5270 INFO: Loading module hook 'hook-distutils.util.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5271 INFO: Loading module hook 'hook-_tkinter.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5450 INFO: checking Tree
    5455 INFO: Building Tree because Tree-00.toc is non existent
    5455 INFO: Building Tree Tree-00.toc
    5473 INFO: checking Tree
    5476 INFO: Building Tree because Tree-01.toc is non existent
    5476 INFO: Building Tree Tree-01.toc
    5587 WARNING: Tcl modules directory /usr/share/tcltk/tcl8.6/../tcl8 does not exist.
    5590 INFO: Loading module hook 'hook-distutils.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5599 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5602 INFO: Loading module hook 'hook-lib2to3.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5616 INFO: Loading module hook 'hook-xml.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5668 INFO: Loading module hook 'hook-difflib.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5670 INFO: Loading module hook 'hook-encodings.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5786 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5786 INFO: Loading module hook 'hook-sysconfig.py' from '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks'...
    5796 INFO: Looking for ctypes DLLs
    5914 INFO: Analyzing run-time hooks ...
    5920 INFO: Including run-time hook '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py'
    5925 INFO: Including run-time hook '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py'
    5928 INFO: Including run-time hook '/home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py'
    5936 INFO: Looking for dynamic libraries
    6395 INFO: Looking for eggs
    6396 INFO: Python library not in binary dependencies. Doing additional searching...
    6447 INFO: Using Python library /lib/x86_64-linux-gnu/libpython3.9.so.1.0
    6462 INFO: Warnings written to /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/build/code00/warn-code00.txt
    6514 INFO: Graph cross-reference written to /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/build/code00/xref-code00.html
    6579 INFO: checking PYZ
    6604 INFO: Building because name changed
    6604 INFO: Building PYZ (ZlibArchive) /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/build/code00/PYZ-00.pyz
    7046 INFO: Building PYZ (ZlibArchive) /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/build/code00/PYZ-00.pyz completed successfully.
    7059 INFO: checking PKG
    7081 INFO: Building because name changed
    7081 INFO: Building PKG (CArchive) code00.pkg
    9827 INFO: Building PKG (CArchive) code00.pkg completed successfully.
    9837 INFO: Bootloader /home/cfati/Work/Dev/VEnvs/py_pc064_03.09_test0/lib/python3.9/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
    9837 INFO: checking EXE
    9869 INFO: Rebuilding EXE-00.toc because code00 missing
    9869 INFO: Building EXE from EXE-00.toc
    9874 INFO: Copying bootloader EXE to /mnt/e/Work/Dev/StackExchange/StackOverflow/q069007351/dist/code00
    9883 INFO: Appending PKG archive to ELF section in EXE
    9995 INFO: Building EXE from EXE-00.toc completed successfully.
    [064bit prompt]>
    [064bit prompt]> ./dist/code00 -h
    Python 3.9.16 (main, Dec  7 2022, 01:11:51) [GCC 9.4.0] 064bit on linux
    
    usage: code00 [-h] [--dummy DUMMY]
    
    q069007351 demo
    
    optional arguments:
      -h, --help            show this help message and exit
      --dummy DUMMY, -d DUMMY
                            Dummy argument
    [064bit prompt]>
    [064bit prompt]> ./dist/code00 -x
    Python 3.9.16 (main, Dec  7 2022, 01:11:51) [GCC 9.4.0] 064bit on linux
    
    Warning: Ignoring unknown arguments: ['-x']
    Args: Namespace(dummy=2.718282)
    parse_args returned: 2.718282
    
    Done.
    
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Thank you @Jiminion, I noticed that you granted me the bounty. So what's the status? Does it work or not? What is / was the problem – CristiFati May 25 '23 at 06:37
0

If there is no necessity in using pyinstaller, py2exe is the workaround.

I just had the same issue with pyinstaller + argparse.

Dummy
  • 51
  • 1
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '23 at 13:54