Python Command Line

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.

Run code
python script.pyRun a Python script file
python3 script.pyRun a script with Python 3 explicitly
python -c "print('hi')"Run a one-line snippet inline
python -m <module>Run a library module as a script
python -Read and run a program from stdin
python -i script.pyRun a script, then stay in an interactive shell
Virtual environments
python -m venv .venvCreate a virtual environment
source .venv/bin/activateActivate it (macOS / Linux)
.venv\Scripts\activateActivate it (Windows)
deactivateLeave the current virtual environment
python -m venv --upgrade .venvUpgrade the venv to the current Python
Handy modules
python -m http.server 8000Serve the current folder over HTTP
python -m json.tool file.jsonPretty-print and validate JSON
python -m pdb script.pyDebug a script with the built-in debugger
python -m timeit "sum(range(100))"Time how fast a snippet runs
python -m calendarPrint a text calendar for the year
python -m zipfile -l archive.zipList the contents of a zip archive
python -m thisPrint the Zen of Python
python -m pip install <pkg>Install a package (see the pip page)
Info & flags
python --versionPrint the Python version
python -VPrint the version (short form)
python -m siteShow module search paths and site info
python -O script.pyRun with basic optimizations (skip asserts)
python -B script.pyDon't write .pyc bytecode files
python -W error script.pyTurn warnings into errors
which pythonShow which python the shell will run

Running 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.

FAQ

How do I create a virtual environment?
Run python -m venv .venv to create one in a .venv folder, then activate it with source .venv/bin/activate on macOS/Linux or .venv\Scripts\activate on Windows. Use deactivate to leave it.
What's the difference between python and python3?
On many systems python3 explicitly runs Python 3, while python may point to Python 2, Python 3, or nothing depending on the machine. Inside an activated virtual environment, python reliably refers to that environment's interpreter.

More cheatsheets