Differences between revisions 1 and 2
Revision 1 as of 2016-01-13 17:43:24
Size: 2314
Editor: JohnWhitlock
Comment: Initial version - json.tool, http.server, and pdb
Revision 2 as of 2019-12-15 07:50:41
Size: 2215
Comment: Update for python 3
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Python 2.4 added the `-m` option to execute modules as scripts ([[https://docs.python.org/3/using/cmdline.html#cmdoption-m|Python 3.x documentation]], [[https://docs.python.org/2/using/cmdline.html#cmdoption-m|2.x documentation]]. [[https://www.python.org/dev/peps/pep-0338/|PEP 338]], adopted in Python 2.5, adds the ability to execute modules in the search path. Python 2.4 added the `-m` option to execute modules as scripts ([[https://docs.python.org/3/using/cmdline.html#cmdoption-m|Python documentation]]). [[https://www.python.org/dev/peps/pep-0338/|PEP 338]], adopted in Python 2.5, adds the ability to execute modules in the search path.
Line 6: Line 6:
 * `python3 -m http.server` or `python2 -m SimpleHTTPServer` - Start a web server, serving files from the current directory ([[https://docs.python.org/3/library/http.server.html#http.server.SimpleHTTPRequestHandler|full Python 3 docs]], [[https://docs.python.org/2/library/simplehttpserver.html|Python 2 docs]]).  * `python3 -m http.server` - Start a web server, serving files from the current directory ([[https://docs.python.org/3/library/http.server.html#http.server.SimpleHTTPRequestHandler|full docs]]).
Line 35: Line 35:
The starting line `#!/usr/bin/env python` is used in UNIX-like shells. If the script is given execute permissions, then it can also be executed directly. The starting line `#!/usr/bin/env python` is used in UNIX-like shells. If the script is given execute permissions, then it can also be executed directly. `python` may still point to Python 2.x on some systems, and in this case `python3` can disambiguate.

Python 2.4 added the -m option to execute modules as scripts (Python documentation). PEP 338, adopted in Python 2.5, adds the ability to execute modules in the search path.

Executable Standard Library Modules

The following standard library modules work as command-line tools:

  • python3 -m http.server - Start a web server, serving files from the current directory (full docs).

  • python -m json.tool path/to/data.json - Validate and pretty-print JSON. Omitting the path will read from standard input (full docs).

  • python -m pdb script.py - Run a script in the debugger, starting post-mortem debugging on unhandled exceptions (full docs).

  • python -m timeit -s 'import string' '"x" in string.lowercase' - Run some setup code (import string), and then run test code many (1,000,000 times) to time execution of the code. (full docs)

Writing Executable Modules

Here's a sample executable Python script:

def main():
    import sys

    for pos, arg in enumerate(sys.argv):
        print('Argument %d: %s' % (pos, arg))


if __name__ == '__main__':
    main()

If this file is importable as mylib.script, then running python -m mylib.script foo bar will output:

Argument 0: /full/path/to/mylib/script.py
Argument 1: foo
Argument 2: bar

The starting line #!/usr/bin/env python is used in UNIX-like shells. If the script is given execute permissions, then it can also be executed directly. python may still point to Python 2.x on some systems, and in this case python3 can disambiguate.

A fully-featured script should use a library to parse the command line, such as argparse. See the source for json.tool for a fuller example.

ExecutableModules (last edited 2019-12-15 07:50:41 by FrancesHocutt)

Unable to edit the page? See the FrontPage for instructions.