Python Profiling and Troubleshooting Cheat Sheet

Profile Python programs, inspect memory and imports, and diagnose slow or stuck processes.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Startup and Import Troubleshooting

Show import statements as they happen

Print detailed import resolution activity.

bashANYimportsstartup
bash
python -v app.py

Print site-package configuration

Inspect interpreter paths and site initialization.

bashANYsite-packagesenvironment
bash
python -m site

Performance Hotspots

Save cProfile output to a file

Store profile stats for later analysis.

bashANYcprofileprofiling
bash
python -m cProfile -o profile.stats app.py

Inspect saved profile stats

Read and sort a saved profile run.

pythonANYpstatscprofile
python
import pstats

stats = pstats.Stats('profile.stats')
stats.sort_stats('cumtime').print_stats(30)

Memory Growth

Show garbage collector stats

Inspect GC activity when memory grows unexpectedly.

pythonANYgcmemory
python
import gc

print(gc.get_count())
print(gc.get_stats())

Force a garbage collection cycle

Useful for experiments while diagnosing leaks.

pythonANYgcmemory
python
import gc

unreachable = gc.collect()
print(unreachable)

Recommended next

No recommendations yet.