The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ: Is there a source code level debugger with breakpoints, stepping into, etc.? Sample code using pdb, ipdb and pudb with tutorial
By Guest on 19th December 2022 05:34:44 AM | Syntax: PYTHON | Views: 213



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. Yes, Python has a built-in debugger called pdb. You can use pdb to set breakpoints, step through code, examine variables, and more.
  2.  
  3. Here's an example of how to use pdb to set a breakpoint and step through some code:
  4.  
  5.  
  6. import pdb
  7.  
  8. def my_function(x, y):
  9.    result = x + y
  10.    pdb.set_trace()  # Set a breakpoint here
  11.    return result
  12.  
  13. print(my_function(1, 2))
  14.  
  15.  
  16. To start the debugger, run the script as usual. When the program execution reaches the breakpoint, the debugger will be launched. You can then use the n command to step through the code, s to step into functions, c to continue execution, and q to quit the debugger. You can also use p to print the value of a variable, l to list the source code, and h to see a list of available commands.
  17.  
  18. There are also several third-party debuggers available for Python, such as pudb and ipdb. These debuggers often have a more user-friendly interface and additional features, such as the ability to view the call stack and inspect the state of all variables in the current scope.
  19.  
  20. Here are some examples of using pudb and ipdb, two popular third-party debuggers for Python:
  21.  
  22.  
  23. # Example using pudb
  24.  
  25. import pudb
  26.  
  27. def my_function(x, y):
  28.    result = x + y
  29.    pudb.set_trace()  # Set a breakpoint here
  30.    return result
  31.  
  32. print(my_function(1, 2))
  33.  
  34. # Example using ipdb
  35.  
  36. import ipdb
  37.  
  38. def my_function(x, y):
  39.    result = x + y
  40.    ipdb.set_trace()  # Set a breakpoint here
  41.    return result
  42.  
  43. print(my_function(1, 2))
  44.  
  45.  
  46.  
  47. To use pudb or ipdb, you will need to install them first using pip. For example:
  48.  
  49.  
  50. pip install pudb
  51. or
  52. pip install ipdb
  53.  
  54. Once installed, you can use pudb.set_trace() or ipdb.set_trace() to set a breakpoint in your code. When the program execution reaches the breakpoint, the debugger will be launched. You can then use the n command to step through the code, s to step into functions, c to continue execution, and q to quit the debugger. pudb and ipdb also provide additional features such as the ability to view the call stack and inspect the state of all variables in the current scope.
  55.  
  56. Watch: python debugger crash course: pdb / breakpoint (beginner - intermediate)
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes