Monitoring Scripts for Errors
Debugging and Fixing Errors
- 3 categories:
- Syntax errors
- occur when the Python parser is unable to understand a line of code
- usually the result of typos or misunderstandings about the Python language syntax
- e.g., forgetting parenthesis
- Runtime errors
- occur during the execution of a program
- often caused by
- operations that are mathematically illegal
- e.g., division by 0
- or attempting to access a resource that isn’t available
- operations that are mathematically illegal
- Semantic errors
- program runs without crashing, but doesn’t produce the expected output
- could be due to error in program’s logic
- Syntax errors
Debugging is the process of identifying and correcting these bugs.
- various tools help with debugging:
- debuggers
- linters
- integrated development environments
Python Debugging
- basic steps:
- Understanding the Problem
- involves reproducing the error and analyzing the error message or incorrect output
- Isolating the Problem
- isolate the section of code causing the error
- done by commenting out sections of code or using print statements to check the values of variables at different stages of the program
- Using a Debugger
- Python comes with a built-in debugger called pdb
- allows stepping through the code line by line, inspecting variables, and setting breakpoints at specific lines of code
- Python comes with a built-in debugger called pdb
- Fixing the Error
- could involve correcting a typo, changing a variable, or rewriting a section of code
- Testing the Solution
- test the solution under different scenarios to ensure the error has been completely resolved
- Understanding the Problem
Common Debugging Techniques
- Print statements
- use print statements to display the values of variables at certain points in the program
- can help identify unexpected values or behavior
- Using a Debugger
- Python’s built-in debugger, pdb, allows stepping through the code line by line, inspecting variables, and setting breakpoints
- powerful tool for understanding the flow of the program and identifying where things go wrong
- Code Review
- can be done individually or as part of a pair programming or code review session
- Unit Testing
- can help catch errors and prevent regressions
- Python’s
unittestmodule provides a framework for creating and running tests
- Logging
- Python’s
loggingmodule can provide valuable insights into the behavior of the program over time
- Python’s
- Profilling
- For performance issues, Python’s
cProfilemodule can help identify bottlenecks in the code
- For performance issues, Python’s
IDE Code Errors
- are issues that arise when writing code in an IDE
- classified as:
- syntax errors
- runtime errors
- semantic errors
- classified as:
- IDEs can flag linting errors
- not a bug per se, but violation of code conventions or best practices
- resolving makes code more readable and maintainable
- each IDE may have different ways of indicating these errors
Debugging Tools
- common debugging tools in Python:
- PDB
- built-in Python debugger
- PyCharm Debugger
- PyCharm, a popular Python IDE, comes with a powerful debugger
- Visual Studio Code Debugger
- popular IDE that includes a versatile debugger with support for remote debugging, multi-threaded debugging, and conditional breakpoints
- Logging
- built-in logging module can be used to record the flow of the program and help identify issues
- Unit Testing Tools
unittest,pytest, anddoctestcan help catch errors and prevent regressions
- Linters
pylintandflake8can catch potential issues in the code that might lead to errors
- Profiling Tools
cProfileandmemory_profilercan help identify performance bottlenecks
- PDB
- The Best Tools to Debug Python
Input Validation
try/except Blocks
try/except blocks are used for exception handling.
- allow the program to continue running even if an error or exception occurs
try:
# Code that might raise an exception
except ExceptionType:
# Code to handle the exception- The
tryblock contains the code that might raise an exception- Python will attempt to execute this code
- If an exception is raised in the
tryblock, the execution immediately moves to the except block- The
ExceptionTypeis the type of exception that theexceptblock can handle - If the exception type matches
ExceptionType, then the code within theexceptblock is executed
- The
- If no exception is raised in the
tryblock, the except block is skipped