The usage of -m option of python

Steven D'Aprano steve+comp.lang.python at
Tue Mar 19 08:06:01 CET 2013

On Mon, 18 Mar 2013 16:17:27 -0500, Peng Yu wrote:

> Hi,
> I don't quite understand how -m option is used. And it is difficult to
> search for -m in google. Could anybody provide me with an example on how
> to use this option? Thanks!
>        -m module-name
>               Searches sys.path for the named module and runs the
> corresponding .py file as a script.

You use it to run a python module or package as a script, without caring 
whether it is a .py file, a .pyc file, a package, compressed in a zip 
file, a module written in C inside a .dll or .so file, or caring exactly 
where it is.

python -m module arguments

is conceptually like:

* launch Python
* insert arguments into sys.argv
* import module
* run it as a script
* exit

So long as the module is *somewhere* on your PYTHONPATH, -m will find it 
and run it. Whether it does something useful or not will depend on the 

Here is one example of a module written to be callable as a script:

[steve at ando ~]$ python -m timeit -s "x = [3, 5, 2, 8, 1, 9, 7]" "x.sort()"
1000000 loops, best of 3: 0.451 usec per loop

Notice that I did not need to worry about where the timeit module 
actually lives on disk. All I needed to know is that it was somewhere in 
the standard library.


More information about the Python-list mailing list