python — The Python Interpreter on the Command Line

Practical guide to the python CLI: run scripts, use the REPL, launch modules with -m, create virtual environments and tune the Python runtime.

The python command is your gateway to the Python interpreter: run a script file, drop into the interactive REPL to try things out, or feed a one-liner straight to the interpreter with -c. Its real superpower is the -m flag, which runs any installed module as a program – from spinning up a quick HTTP server (http.server) to creating a virtual environment (venv) or invoking pip. This guide collects the flags and modules you reach for daily, whether you are scripting, debugging, profiling or just exploring.

Run Scripts

python <script> — Execute a Python script.

python app.py

python <script> <args> — Run a script with command-line arguments.

python convert.py --input data.csv --output result.json

python -c '<code>' — Execute a Python expression directly.

python -c 'import json; print(json.dumps({"key": "value"}, indent=2))'

python -m <module> — Run a module as a script.

python -m http.server 8080

python -i <script> — Run script then enter interactive mode (for debugging).

python -i setup.py

Interactive & REPL

python — Start the interactive Python REPL.

python

python -q — Start REPL in quiet mode (no version banner).

python -q

python -m asyncio — Start an async-aware REPL (supports await).

python -m asyncio

Debugging & Profiling

python -m pdb <script> — Run script with the Python debugger.

python -m pdb app.py

python -m cProfile <script> — Profile a script (function-level timing).

python -m cProfile -s cumtime app.py

python -m timeit '<code>' — Benchmark a code snippet.

python -m timeit 'sum(range(1000))'

python -m trace --trace <script> — Trace execution of every line.

python -m trace --trace app.py

python -W all <script> — Show all warnings.

python -W all app.py

python -X tracemalloc <script> — Enable memory allocation tracing.

python -X tracemalloc app.py

Useful Modules

python -m http.server <port> — Start a simple HTTP file server.

python -m http.server 8000

python -m json.tool <file> — Pretty-print JSON from a file or stdin.

echo '{"a":1}' | python -m json.tool

python -m zipfile -c <zip> <files> — Create a zip archive.

python -m zipfile -c archive.zip *.py

python -m base64 <file> — Base64 encode a file.

python -m base64 image.png

python -m calendar <year> — Display a calendar for a year.

python -m calendar 2026

python -m site — Show Python path configuration and site-packages locations.

python -m site

Virtual Environments

python -m venv <dir> — Create a virtual environment.

python -m venv .venv

source <dir>/bin/activate — Activate the virtual environment (Linux/macOS).

source .venv/bin/activate

<dir>\Scripts\activate — Activate the virtual environment (Windows).

.venv\Scripts\activate

deactivate — Deactivate the current virtual environment.

deactivate

python -m venv --clear <dir> — Reset an existing virtual environment.

python -m venv --clear .venv

Info & Options

python --version — Show Python version.

python --version

python -V -V — Show detailed version info (version, build, compiler).

python -V -V

python -m sysconfig — Show Python build configuration.

python -m sysconfig

python -O <script> — Run with basic optimizations (remove assert statements).

python -O app.py

python -B <script> — Don't write .pyc bytecode files.

python -B app.py

python -u <script> — Unbuffered stdout/stderr (useful for logging).

python -u worker.py

Conclusion

The python CLI is far more than a way to launch scripts: with -m it turns the standard library into a toolbox of ready-made utilities, and the REPL stays the fastest place to test an idea. Keep one habit in mind for safety – -c and -m execute arbitrary code, so only run snippets and modules you trust. Treat python -m http.server as a local development helper, never a production web server, and prefer a project-local virtual environment (python -m venv) over installing packages into the system Python. Once these flags become muscle memory, the interpreter turns into a genuinely versatile everyday tool.

Further Reading

  • artisan – Laravel's PHP command-line tool for scaffolding and tasks
  • cargo – build system and package manager for Rust
  • composer – dependency manager for PHP projects