These are Python’s most helpful general-purpose command-line tools.

CommandPurposeMore
python -m http.serverStart a simple web serverVideo
python -m webbrowserLaunch your web browserDocs
python -m json.toolNicely format JSON dataDocs
python -m calendarShow a command-line calendarDocs

http.server

Running the http.server module as a script will start a web server on port 8000 that hosts files from the current directory. I use this all the time to preview Sphinx documentation sites (especially when using Sphinx’s dirhtml option which is all about subdirectories of index.html files).

$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

webbrowser

Running the webbrowser module as a script will open a given URL in your default web browser. For example, this would open the page https://pseudorandom.name:

$ python -m webbrowser pseudorandom.name

json.tool

Python’s json.tool module can be run as a script to parse a JSON document and print out a version that’s formatted nicely for human readability.

$ python -m json.tool /home/trey/Downloads/download.json
[
    {
        "title": "Python's walrus operator",
        "is_premium": false,
        "url": "/using-walrus-operator/"
    },
    {
        "title": "Refactoring long boolean expressions",
        "is_premium": true,
        "url": "/refactoring-boolean-expressions/"
    }
]

calendar

Running the calendar module as a script will print a calendar of the current year by default. It also accepts various arguments to customize its output. Here’s a calendar of just one month:

$ python -m calendar 2024 04
     April 2024
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

Those 4 scripts are general-purpose tools that I find helpful on any machine. Python also includes a number of tools that are commonly available (or easily installable) on Linux and Mac machines.

Especially handy on Windows machines

Running Python on Windows? Or running Python on a Linux/Mac machine without the ability to easily install common command-line utilities like uuid, sqlite3 and gzip?

These tools are all equivalent to command-line tools that are common on many Linux machines, though the equivalent Linux commands are usually more powerful and more user-friendly.

CommandPurposeMore
python3.12 -m uuidLike uuidgen CLI utilityDocs
python3.12 -m sqlite3Like sqlite3 CLI utilityDocs
python -m zipfileLike zip & unzip CLI utilitiesDocs
python -m gzipLike gzip & gunzip CLI utilitiesDocs
python -m tarfileLike the tar CLI utilityDocs
python -m base64Like the base64 CLI utility
python -m ftplibLike the ftp utility
python -m smtplibLike the sendmail utility
python -m poplibLike using curl to read email
python -m imaplibLike using curl to read email
python -m telnetlibLike the telnetutility

Analyzing Python code

Python also includes a handful of other Python-related tools that are specifically for analyzing Python code.

If you wanted to analyze some Python code to see how it ticks, these tools can be useful.

CommandPurposeMore
python -m tokenizeBreak Python module into “tokens”Docs
python -m astShow abstract syntax tree for codeDocs
python -m disDisassemble Python code to bytecodeDocs
python -m inspectinspect source code of a Python objectDocs
python -m pyclbrSee overview of a module’s objects

Just for fun

These are Python Easter Eggs that work as Python scripts.

CommandPurpose
python -m __hello__Print Hello world!
python -m thisDisplay the Zen of Python (PEP 20)
python -m antigravityOpen XKCD 353 in a web browser
python -m turtledemoSee turtle module demos