Author terry.reedy
Recipients Jonathan Huot, docs@python, ncoghlan, terry.reedy
Date 2018-03-24.01:15:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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/ -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\\' versus 'f:/python/a/']

Unless 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 file is never the command unless invoked by its full path: "python somepath/".  In such a case, sys.argv access should be within a "__name__ == '__main__':" clause or a function called therein.
Date User Action Args
2018-03-24 01:15:22terry.reedysetrecipients: + terry.reedy, ncoghlan, docs@python, Jonathan Huot
2018-03-24 01:15:22terry.reedysetmessageid: <>
2018-03-24 01:15:21terry.reedylinkissue33119 messages
2018-03-24 01:15:19terry.reedycreate