| [ Web Proxy ] |
| Viewing: https://wiki.python.org/moin/../python/platforms/../../python/gui/../archive/ExecutableModules.html | [Back] [Original] |
Legacy Wiki Page
This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.
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.
The following standard library modules work as command-line tools:
python3-mhttp.server - Start a web server, serving files from the current directory (full docs).
python-mjson.toolpath/to/data.json - Validate and pretty-print JSON. Omitting the path will read from standard input (full docs).
python-mpdbscript.py - Run a script in the debugger, starting post-mortem debugging on unhandled exceptions (full docs).
python-mtimeit-s'importstring''"x"instring.lowercase' - Run some setup code (importstring), and then run test code many (1,000,000 times) to time execution of the code. (full docs)
Heres 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-mmylib.scriptfoobar will output:
Argument 0: /full/path/to/mylib/script.py
Argument 1: foo
Argument 2: bar
The starting line #!/usr/bin/envpython 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.
Back to top2026, Python Software Foundation
Made with Sphinx and Shibuya theme.
| Web Proxy Viewer | New URL | Original Page |