argparse displays __main__.py instead of executable module name

Viewed 770

When using argparse in an executable module, the help message displays __main__.py instead of the module name. How to correct this?

$python -m mymodule -h

usage: __main__.py [-h]`

My executable python module

optional arguments:
  -h, --help  show this help message and exit
1 Answers

The instruction python -m mymodule runs the __main__.py script located in mymodule, this is why argparse uses __main__.py as program name (doc).

The program name can be overridden with the prog argument:

parser = argparse.ArgumentParser(
        prog='python -m mymodule',
        description='My executable python module.')

indeed:

$python -m mymodule -h

usage: python -m mymodule [-h]

My executable python module

optional arguments:
  -h, --help  show this help message and exit
Related