Python CLI Interpreter and -m tools
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
python3 <script.py> [args]
python3python3 -c "<code>"
python3 -m <module>
python3 -m venv .venv
python3 -m pip install <pkg>
python3 -i <script.py>
python3 -m http.server
Concepts
- python3 <file> script, -m or -c
- Bytecode cached in __pycache__
- Interpreter sys.path finds imports
- Exit code 0 = success
Run code
python3 <script.py> <args>
python3 -c "<code>"
python3 -m <module> <args>
python3 -VInteractive
python3python3 -i <script.py>
_help(<obj>)
dir(<obj>)
Ctrl+DPYTHONSTARTUP=<file>
F2F3Virtual envs and packages
python3 -m venv .venv
source .venv/bin/activate
.venv\Scripts\activate
deactivate.venv/bin/python <script.py>
python3 -m venv --clear <dir>
python3 -m pip install <pkg>
python3 -m pip install -r requirements.txt
python3 -m pip install -e .
Dev servers and tools
python3 -m http.server
python3 -m http.server <port>
python3 -m http.server -b 127.0.0.1
python3 -m json.tool <file>
Test and debug
python3 -m pdb <script.py>
python3 -m pdb -c continue <script.py>
breakpoint()n / s / c / ql / ll / wpython3 -m unittest
python3 -X faulthandler <script.py>
python3 -X dev <script.py>
Measure
python3 -m timeit "<stmt>"
python3 -m timeit -s "<setup>"
"<stmt>"
python3 -m timeit -n <n> -r <r>
"<stmt>"
python3 -m cProfile -s cumtime <script.py>
python3 -X importtime <script.py>
Flags and behaviour
python3 -u <script.py>
python3 -O <script.py>
python3 -I <script.py>
python3 --help-env
Environment variables
PYTHONPATH=<dir>[:<dir>]
PYTHONUNBUFFERED=1
| Order | Source |
|---|---|
| 1 | Script dir (or cwd with -c / -m) |
| 2 | PYTHONPATH entries |
| 3 | Standard library |
| 4 | site-packages |
Recipes
python3 -m venv .venvsource .venv/bin/activatepython -m pip install -r requirements.txtpython -m pytest
cd ~/Publicpython3 -m http.server 8080# then open http://<your-ip>:8080
curl -s <url> | python3 -m json.tool# keep non-ASCII text readablecurl -s <url> | python3 -m json.tool --no-ensure-ascii
python3 -m pdb -c continue app.pypython3 -m cProfile -s cumtime app.py | head -20python3 -m timeit -s "import json" "json.dumps({'a': 1})"



