Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse displays __main__.py instead of executable module name

Tags:

python

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
like image 840
MarcoP Avatar asked Apr 27 '26 23:04

MarcoP


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
like image 50
MarcoP Avatar answered Apr 30 '26 12:04

MarcoP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!