python commands cheatsheet.
Run scripts and modules, spin up virtual environments, and reach for handy stdlib modules — the Python interpreter commands you use every day. Tap to copy.
python script.pyRun a Python script filepython3 script.pyRun a script with Python 3 explicitlypython -c "print('hi')"Run a one-line snippet inlinepython -m <module>Run a library module as a scriptpython -Read and run a program from stdinpython -i script.pyRun a script, then stay in an interactive shellpython -m venv .venvCreate a virtual environmentsource .venv/bin/activateActivate it (macOS / Linux).venv\Scripts\activateActivate it (Windows)deactivateLeave the current virtual environmentpython -m venv --upgrade .venvUpgrade the venv to the current Pythonpython -m http.server 8000Serve the current folder over HTTPpython -m json.tool file.jsonPretty-print and validate JSONpython -m pdb script.pyDebug a script with the built-in debuggerpython -m timeit "sum(range(100))"Time how fast a snippet runspython -m calendarPrint a text calendar for the yearpython -m zipfile -l archive.zipList the contents of a zip archivepython -m thisPrint the Zen of Pythonpython -m pip install <pkg>Install a package (see the pip page)python --versionPrint the Python versionpython -VPrint the version (short form)python -m siteShow module search paths and site infopython -O script.pyRun with basic optimizations (skip asserts)python -B script.pyDon't write .pyc bytecode filespython -W error script.pyTurn warnings into errorswhich pythonShow which python the shell will runRunning Python from the command line
The python (or python3) interpreter is the front door to everything Python does. The most common thing you'll type is python script.py to run a file, but the interpreter has a few other modes worth knowing. python -c "…" runs a snippet inline without a file, python - reads a program piped in from stdin, and python -i script.py runs a script and then drops you into an interactive shell with all its variables still loaded — perfect for poking at the result.
Modules with -m
The -m flag runs a library module as if it were a script, and it's the secret behind some of Python's most useful one-liners. python -m http.server 8000 spins up a static file server in the current folder, python -m json.tool file.json pretty-prints and validates JSON, and python -m pdb script.py launches the built-in debugger. Because these ship with Python's standard library, they work anywhere Python does — no installation required.
Virtual environments
Installing packages into the system Python leads to conflicts between projects, which is why virtual environments exist — isolated spaces where each project keeps its own dependencies. Create one with python -m venv .venv, then activate it with source .venv/bin/activate on macOS and Linux or .venv\Scripts\activate on Windows. Once activated, plain python reliably points at that environment's interpreter, and deactivate returns you to the system default.
Flags and versions
A handful of flags shape how the interpreter behaves. python --version (or python -V) prints which Python you're running, python -O strips out assert statements for a small speedup, and python -B skips writing .pyc bytecode files. For managing the packages inside an environment, see the pip & conda commands; these pair naturally with the Git commands for version control and the Linux commands in your shell.