python -m lets you run modules as scripts. If your module is just one .py file it'll be executed (which usually means code under
if __name__ == '__main__'). If your module is a directory, Python will look for
__main__.py (next to
__init__.py) and will run it.
One of Python's mottoes is "batteries included", and this goes for
python -m as well. Here are some (all?) of the gems hidden in the standard library. Sadly not all of them have help, but I poked around in the source code to see the usage.
json.tool
This is by far the one I use most, it'll indent nicely an JSON input in the standard output and very helpful in combination with curl.
$ curl -sL http://j.mp/1IuxaLD
[{"x":1,"y":2},{"x":3,"y":4},{"x":5,"y":6}]
$ curl -sL http://j.mp/1IuxaLD | python -m json.tool
[
{
"x": 1,
"y": 2
},
{
"x": 3,
"y": 4
},
{
"x": 5,
"y": 6
}
]
zipfile
zipfile will let you view, extract and create zip files - very much like the zip and unzip. Here's the help:
$ python -m zipfile -h
Usage:
zipfile.py -l zipfile.zip # Show listing of a zipfile
zipfile.py -t zipfile.zip # Test if a zipfile is valid
zipfile.py -e zipfile.zip target # Extract zipfile into target dir
zipfile.py -c zipfile.zip src ... # Create zipfile from sources
gzip
Like zipfile, let's you compress and decompress .gz files, like gzip/gunzip. By default it'll compress a file but with -d will decompress.
python -m gzip wordlist.txt # Will create wordlist.txt.gz
python -m gzip -d wordlist.txt.gz # Will extract to wordlist.txt
filecmp
Compare two directories.
$ python -m filecmp /tmp/a /tmp/b
diff /tmp/a /tmp/b
Only in /tmp/a : ['1']
Only in /tmp/b : ['2']
Identical files : ['4']
Differing files : ['3']
Encode/Decode
Several modules lets you encode/decode in various formats:
- base64
- uu
- encodings.rot_13
- binhex
- mimify
- quopri
For example
$ echo 'secertpassword' | python -m encodings.rot_13
frpregcnffjbeq
Servers
There are several servers that you can run, the ones I know are SimpleHTTPServer, CGIHTTPServer and smtpd (mail). If you quickly want to serve some files from a directory on your machine, just run:
python -m SimpleHTTPServer
Clients
Modules that provide simple clients to various protocols are:
- ftplib
- poplib
- nntplib
- smtplib (on localhost only)
- telnetlib
For example if you want to view Star Wars in text mode, do
$ python -m telnetlib towel.blinkenlights.nl
System Info
You can use platform to get some platform information (very much line uname -a) and locale to get locale information. Use mimetype to get the mime type of a file:
$ python -m mimetypes doc.html
type: text/html encoding: None
Python Utilties
- compileall will compile all Python files to .pyc
- dis will show bytecode for a file
- pdb will start the Python debugger on a given file (see here)
- pydoc will show documentation on a module/class/function
- site will print some site information (sys.path, USER_BASE ...)
- sysconfig will show many system related information (such as exec_prefix)
- tabnanny will tell you of you mix tabs and spaces (like starting python with -t or -tt)
- tokenize will print list of tokens in Python file
I mostly use pdb and pydoc, for example:
$ python -m pydoc os.remove
Help on built-in function remove in os:
os.remove = remove(...)
remove(path)
Remove a file (same as unlink(path)).
Profiling
There are several profiles and timers you can use from the command line:
- cProfile - Show profile information
- profile (use cProfile :)
- timeit - Time how long things run
- pstats - Print output of profiles
- trace - Show tracing information on run
Example:
$ python -m cProfile match.py
28537 function calls (27503 primitive calls) in 0.057 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :1()
1 0.000 0.000 0.000 0.000 :1(ArgInfo)
1 0.000 0.000 0.000 0.000 :1(ArgSpec)
...
$ pyton -m timeit 'import math; math.factorial(100)'
100000 loops, best of 3: 12.9 usec per loop
timeit has good help from the command line.
IDLE
You can start IDLE by running python -m idlelib.idle
ensurepip
Python 2.7.9 and 3.x comes with an easy way to install pip. Run python -m ensurepip and pypi is at your service.
That's about it ... What are you favorite python -m tools? Which ones did I miss?
EDIT: The good folks at comp.lang.python reminded me a few I forgot:
unittest
python -m unittest discover will run unittest in discovery mode. Just drop a new Python file starting with test and it'll be picked up next time you run the tests. You can also specify a specific test to run with python -m unittest test_file.py TestClass.test_method.
calendar
python -m calendar will show calendar of the current year. You can also run python -m calendar YEAR to display a specific year and python -m calendar YEAR MONTH to display a specific month.
Easter Eggs
python -m this will display the Python Zen
python -m antigravity will open XKCD comic web page (which my company is named after).