Author Jonathan Huot
Recipients Jonathan Huot, docs@python
Date 2018-03-22.09:21:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Executing python modules with -m can lead to weird sys.argv parsing.

"Argument parsing" section at mention :

- When -m module is used, sys.argv[0] is set to the full name of the located module.

The word "located" is used, but it doesn't mention anything when the module is not *yet* "located".

For instance, let's see what is the sys.argv for each python files:

$ cat mainmodule/
import sys; print("{}: {}".format(sys.argv, __file__))
$ cat mainmodule/submodule/
import sys; print("{}: {}".format(sys.argv, __file__))
$ cat mainmodule/submodule/
import sys; print("{}: {}".format(sys.argv, __file__))

Then we call "foobar" with -m:

$ python -m mainmodule.submodule.foobar -o -b
['-m', '-o', 'b']: (..)/mainmodule/
['-m', '-o', 'b']: (..)/mainmodule/submodule/
['(..)/mainmodule/submodule/', '-o', 'b']: (..)/mainmodule/submodule/

We notice that only "-m" is in sys.argv before we found "foobar". This can lead to a lot of troubles when we have meaningful processing in which rely on sys.argv to initialize stuff.

IMHO, it either should be the sys.argv intact ['-m', 'mainmodule.submodule.foobar', '-o', '-b'] or empty ['', '-o', '-b'] or  only the latest ['-o', '-b'], but it should not be ['-m', '-o', '-b'] which is very confusing.
Date User Action Args
2018-03-22 09:21:13Jonathan Huotsetrecipients: + Jonathan Huot, docs@python
2018-03-22 09:21:13Jonathan Huotsetmessageid: <>
2018-03-22 09:21:13Jonathan Huotlinkissue33119 messages
2018-03-22 09:21:12Jonathan Huotcreate