[issue33119] python sys.argv argument parsing not clear

Terry J. Reedy report at bugs.python.org
Fri Mar 23 21:15:22 EDT 2018


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Two of your 3 suggested alternatives could lead to bugs. To use your example:
 python -m mainmodule.submodule.foobar -o -b
is a convenient alternative and abbreviation for
 python .../somedir/mainmodule/submodule/foobar.py -o -b
The two invocations should give equivalent results and to the extent possible the same result.

[What might be different is the form of argv[0].  In the first case, argv[0] will be the "preferred" form of the path to the python file while in the second, it will be whatever is given.  On Windows, the difference might look like 'F:\\Python\\a\\tem2.py' versus 'f:/python/a/tem2.py']

Unless __init__.py does some evil monkeypatching, it cannot affect the main module unless imported directly or indirectly.  So its behavior should be the same whether imported before or after execution of the main module.  This means that argv must be the same either way (except for argv[0]).  So argv[0:2] must be condensed to one arg before executing __init__.  I don't see that '' is an improvement over '-m'.

Command line arguments are intended for the invoked command.  An __init__.py file is never the command unless invoked by its full path: "python somepath/__init__.py".  In such a case, sys.argv access should be within a "__name__ == '__main__':" clause or a function called therein.

----------
nosy: +terry.reedy
versions:  -Python 3.4, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33119>
_______________________________________


More information about the Python-bugs-list mailing list