Debugging Fundamentals
Debugging is the process of identifying and resolving errors in your code. Python provides several tools and techniques to help you debug effectively.
# Common types of bugs:
# 1. Syntax errors - caught by Python interpreter
# 2. Runtime errors - occur during execution
# 3. Logical errors - code runs but produces wrong results
# Basic debugging approach:
# 1. Reproduce the error
# 2. Isolate the problem area
# 3. Identify the root cause
# 4. Fix and test the solution
Effective debugging requires a systematic approach and familiarity with Python's debugging tools.
Print Debugging
The simplest debugging technique - using print statements to track program flow and variable values.
def calculate_average(numbers):
print(f"Debug: numbers = {numbers}") # Check input
total = 0
for num in numbers:
print(f"Debug: adding {num} to total ({total})")
total += num
average = total / len(numbers)
print(f"Debug: average = {average}") # Check calculation
return average
# Better alternative: use logging module
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def better_calculate_average(numbers):
logger.debug(f"Numbers: {numbers}")
# ... rest of function ...
While simple, print debugging can become messy. Consider using the logging module for more control over debug output.
Python Debugger (pdb)
Python's built-in debugger allows interactive debugging with breakpoints and step-by-step execution.
# Starting the debugger:
# 1. Import pdb and add breakpoint
import pdb
def buggy_function(x, y):
result = x + y
pdb.set_trace() # Execution pauses here
result *= 2
return result
# Common pdb commands:
# l (list) - Show current position in code
# n (next) - Execute next line
# s (step) - Step into function
# c (continue) - Continue execution
# p (print) - Print variable value
# q (quit) - Quit debugger
# Python 3.7+ alternative: breakpoint()
def modern_function():
breakpoint() # Same as pdb.set_trace()
# ...
IDE Debugging Tools
Modern IDEs provide powerful graphical debugging interfaces.
# VS Code Debugging:
# 1. Set breakpoints by clicking left gutter
# 2. Use Debug View (Ctrl+Shift+D)
# 3. Start debugging with F5
# 4. Use debug toolbar to step through code
# PyCharm Debugging:
# 1. Click left gutter to set breakpoints
# 2. Right-click → Debug 'filename'
# 3. Use debug toolbar (Step Over, Into, Out)
# 4. Inspect variables in Debug panel
# Common IDE debug features:
# - Conditional breakpoints
# - Watch expressions
# - Call stack inspection
# - Variable value modification
Advanced Debugging Techniques
For complex debugging scenarios, Python offers more advanced tools.
# Post-mortem debugging
import pdb
import sys
def buggy_function():
try:
# Code that might fail
1 / 0
except:
# Enter debugger after exception
pdb.post_mortem(sys.exc_info()[2])
# Debugging from command line:
# python -m pdb script.py
# Starts debugger before running script
# Remote debugging with pdb++:
import pdbp # pip install pdbp
pdbp.set_trace() # Enhanced debugger
# Debugging with ipdb (IPython debugger):
import ipdb # pip install ipdb
ipdb.set_trace() # More features than pdb
Specialized Debugging Tools
Python ecosystem offers specialized tools for different debugging needs.
# PySnooper - Automatic print debugging
import pysnooper # pip install pysnooper
@pysnooper.snoop()
def snooped_function(x):
result = []
for i in range(x):
result.append(i * 2)
return result
# Icecream - Better print debugging
from icecream import ic # pip install icecream
ic(len([1, 2, 3])) # ic| len([1, 2, 3]): 3
# Debugging memory issues with tracemalloc:
import tracemalloc
tracemalloc.start()
# ... run code ...
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats[:10]:
print(stat)
# Profiling with cProfile:
# python -m cProfile -s time script.py
Python Debugging Videos
Master Python debugging with these handpicked YouTube tutorials:
Core debugging techniques:
Debugging in development environments:
Specialized debugging utilities:
Finding and fixing performance issues: