[Python-ideas] Cleanup, uniformization and documentation of stdlib module reactions to `python -m <module>`

Masklinn masklinn at masklinn.net
Sat Feb 19 18:57:13 CET 2011


There are three ways a Python module can react to `-m`: do nothing, selftest (via doctests or via python code) and do something "useful" (for Python users).

The former is fine, but the latter two have more issues:

For modules which selftest, the self-tests/sanity check are generally undocumented and potentially long-running or destructive. It would be nice if those were protected behind an argument (e.g. `-x` for `—execute`) just in case

For "useful" modules, the issues are far more complex:

1. Many (if not most) are under- or un-documented, no information being available in their module documentation, and/or via the command-line (ftplib, modulefinder, dis), this makes them into little more than easter eggs. Some are documented in the standard library documentation but not the command-line (http.server), others are documented on the command-line but not in their module documentation (smtpd). It would be nice if all of these tools were documented in both places, but the bare minimum would be for all of them to provide CLI help (one which explains at the very least what the command does, here again some of the commands providing a "cli help" give little more information than a list of options: no information on the command itself, and no documentation of what the various options do).

2. The arguments parsing is inconsistent, I've seen all of manual parsing (ftplib, http.server, pstats), getopt (smtpd, timeit, trace, webbrowser), optparse (calendar, cProfile, uu) and argparse (compileall, nntplib), and the reactions to standard flags (e.g. `-h`/`—help`) is a crapshoot, modules may or may not crash completely or display parse errors (when provided with a `-h` flag). All modules should use the same arguments parsing library (argparse), which would automatically yield a correct reaction to `-h` across the board. Even modules not using options themselves (e.g. http.server) should do so, so they can react correctly to `-h`.

There are many hidden gems in the stdlib, and CLI usage of modules is one of them: some are pretty well-known (timeit, http.server), others are more rarely known (urllib, cProfiler) but I'd wager the vast majority are almost never used because the vast majority of Python developers have no idea they can be used that way.


More information about the Python-ideas mailing list