Sunday, 15 May 2011

Why does (python -m ...) result in sys.argv like ['-c', ...] -



Why does (python -m ...) result in sys.argv like ['-c', ...] -

i have simple script test.py

import sys print sys.argv

if run

python test.py abcdin cmd,

i ['test.py', 'abcd'].

however, if run

python -m test.py abcd

i

['-c', 'abcd'] e:\program files (x86)\py\python.exe: no module named test.py.

why '-c' here? searched stack overflow utilize of '-m' still confused.

the -m switch tells python module or package import , run it's __main__ entry. see -m switch documentation:

search sys.path named module , execute contents __main__ module.

for case, means python looking package called test.py (note: not file name). find it, python first imports module test (from test.py file), sub-module named py. fails. in process test module still run , prints out sys.argv.

the -c switch in sys.argv list internal implementation detail of how process works. see python issue #8202 of details:

as recall, used bit of hack main.c implement -m correctly piggybacking on existing -c semantics. i'll find hack , replace proper '-c' or '-m' logic.

if want utilize -m switch you'll need drop .py part; that's file extension, not module name:

python -m test abcd

python

No comments:

Post a Comment