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

Source: https://www.jpkc.com/db/en/cheatsheets/build-languages/python/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Run Scripts

`python <script>` — Execute a Python script.

```bash
python app.py
```

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

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

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

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

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

```bash
python -m http.server 8080
```

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

```bash
python -i setup.py
```

## Interactive & REPL

`python` — Start the interactive Python REPL.

```bash
python
```

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

```bash
python -q
```

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

```bash
python -m asyncio
```

## Debugging & Profiling

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

```bash
python -m pdb app.py
```

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

```bash
python -m cProfile -s cumtime app.py
```

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

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

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

```bash
python -m trace --trace app.py
```

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

```bash
python -W all app.py
```

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

```bash
python -X tracemalloc app.py
```

## Useful Modules

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

```bash
python -m http.server 8000
```

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

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

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

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

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

```bash
python -m base64 image.png
```

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

```bash
python -m calendar 2026
```

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

```bash
python -m site
```

## Virtual Environments

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

```bash
python -m venv .venv
```

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

```bash
source .venv/bin/activate
```

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

```bash
.venv\Scripts\activate
```

`deactivate` — Deactivate the current virtual environment.

```bash
deactivate
```

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

```bash
python -m venv --clear .venv
```

## Info & Options

`python --version` — Show Python version.

```bash
python --version
```

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

```bash
python -V -V
```

`python -m sysconfig` — Show Python build configuration.

```bash
python -m sysconfig
```

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

```bash
python -O app.py
```

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

```bash
python -B app.py
```

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

```bash
python -u worker.py
```

<!-- PROSE:outro -->
## 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](https://docs.python.org/3/) – the official reference for the language and standard library
- [Command line and environment](https://docs.python.org/3/using/cmdline.html) – complete list of interpreter options and environment variables
- [venv – virtual environments](https://docs.python.org/3/library/venv.html) – creating and managing isolated environments
<!-- PROSE:outro:end -->

## Related Commands

- [artisan](https://www.jpkc.com/db/en/cheatsheets/build-languages/artisan/) – Laravel's PHP command-line tool for scaffolding and tasks
- [cargo](https://www.jpkc.com/db/en/cheatsheets/build-languages/cargo/) – build system and package manager for Rust
- [composer](https://www.jpkc.com/db/en/cheatsheets/build-languages/composer/) – dependency manager for PHP projects

