Could you explain to me what the difference is between calling
python -m mymod1 mymod2.py args
and
python mymod1.py mymod2.py args
It seems in both cases mymod1.py
is called and sys.argv
is
['mymod1.py', 'mymod2.py', 'args']
So what is the -m
switch for?
The first line of the Rationale
section of PEP 338 says:
Python 2.4 adds the command line switch
-m
to allow modules to be located using the Python module namespace for execution as scripts. The motivating examples were standard library modules such aspdb
andprofile
, and the Python 2.4 implementation is fine for this limited purpose.
So you can specify any module in Python's search path this way, not just files in the current directory. You're correct that python mymod1.py mymod2.py args
has exactly the same effect. The first line of the Scope of this proposal
section states:
In Python 2.4, a module located using
-m
is executed just as if its filename had been provided on the command line.
With -m
more is possible, like working with modules which are part of a package, etc. That's what the rest of PEP 338 is about. Read it for more info.