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.pypython <script> <args> — Run a script with command-line arguments.
python convert.py --input data.csv --output result.jsonpython -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 8080python -i <script> — Run script then enter interactive mode (for debugging).
python -i setup.pyInteractive & REPL
python — Start the interactive Python REPL.
pythonpython -q — Start REPL in quiet mode (no version banner).
python -qpython -m asyncio — Start an async-aware REPL (supports await).
python -m asyncioDebugging & Profiling
python -m pdb <script> — Run script with the Python debugger.
python -m pdb app.pypython -m cProfile <script> — Profile a script (function-level timing).
python -m cProfile -s cumtime app.pypython -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.pypython -W all <script> — Show all warnings.
python -W all app.pypython -X tracemalloc <script> — Enable memory allocation tracing.
python -X tracemalloc app.pyUseful Modules
python -m http.server <port> — Start a simple HTTP file server.
python -m http.server 8000python -m json.tool <file> — Pretty-print JSON from a file or stdin.
echo '{"a":1}' | python -m json.toolpython -m zipfile -c <zip> <files> — Create a zip archive.
python -m zipfile -c archive.zip *.pypython -m base64 <file> — Base64 encode a file.
python -m base64 image.pngpython -m calendar <year> — Display a calendar for a year.
python -m calendar 2026python -m site — Show Python path configuration and site-packages locations.
python -m siteVirtual Environments
python -m venv <dir> — Create a virtual environment.
python -m venv .venvsource <dir>/bin/activate — Activate the virtual environment (Linux/macOS).
source .venv/bin/activate<dir>\Scripts\activate — Activate the virtual environment (Windows).
.venv\Scripts\activatedeactivate — Deactivate the current virtual environment.
deactivatepython -m venv --clear <dir> — Reset an existing virtual environment.
python -m venv --clear .venvInfo & Options
python --version — Show Python version.
python --versionpython -V -V — Show detailed version info (version, build, compiler).
python -V -Vpython -m sysconfig — Show Python build configuration.
python -m sysconfigpython -O <script> — Run with basic optimizations (remove assert statements).
python -O app.pypython -B <script> — Don't write .pyc bytecode files.
python -B app.pypython -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
- Python documentation – the official reference for the language and standard library
- Command line and environment – complete list of interpreter options and environment variables
- venv – virtual environments – creating and managing isolated environments