Yes! There's a Python debugger called pdb just for doing that!
You can launch a Python program through pdb via python -m pdb myscript.py.
There are a few commands you can then issue, which are documented on the pdb page.
Some useful ones to remember are:
b: set a breakpointc: continue debugging until you hit a breakpoints: step through the coden: to go to next line of codel: list source code for the current file (default: 11 lines including the line being executed)u: navigate up a stack framed: navigate down a stack framep: to print the value of an expression in the current context
If you don't want to use a command line debugger, some IDEs like Pydev, Wing IDE or PyCharm have a GUI debugger. Wing and PyCharm are commercial products, but Wing has a free "Personal" edition, and PyCharm has a free community edition.
Answer from user193476 on Stack OverflowYes! There's a Python debugger called pdb just for doing that!
You can launch a Python program through pdb via python -m pdb myscript.py.
There are a few commands you can then issue, which are documented on the pdb page.
Some useful ones to remember are:
b: set a breakpointc: continue debugging until you hit a breakpoints: step through the coden: to go to next line of codel: list source code for the current file (default: 11 lines including the line being executed)u: navigate up a stack framed: navigate down a stack framep: to print the value of an expression in the current context
If you don't want to use a command line debugger, some IDEs like Pydev, Wing IDE or PyCharm have a GUI debugger. Wing and PyCharm are commercial products, but Wing has a free "Personal" edition, and PyCharm has a free community edition.
By using Python Interactive Debugger 'pdb'
First step is to make the Python interpreter enter into the debugging mode.
A. From the Command Line
Most straight forward way, running from command line, of python interpreter
$ python -m pdb scriptName.py
> .../pdb_script.py(7)<module>()
-> """
(Pdb)
B. Within the Interpreter
While developing early versions of modules and to experiment it more iteratively.
$ python
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb_script
>>> import pdb
>>> pdb.run('pdb_script.MyObj(5).go()')
> <string>(1)<module>()
(Pdb)
C. From Within Your Program
For a big project and long-running module, can start the debugging from inside the program using
import pdb and set_trace()
like this:
#!/usr/bin/env python
# encoding: utf-8
#
import pdb
class MyObj(object):
count = 5
def __init__(self):
self.count= 9
def go(self):
for i in range(self.count):
pdb.set_trace()
print i
return
if __name__ == '__main__':
MyObj(5).go()
Step-by-Step debugging to go into more internal
Execute the next statement… with “n” (next)
Repeating the last debugging command… with ENTER
Quitting it all… with “q” (quit)
Printing the value of variables… with “p” (print)
a)
p aTurning off the (Pdb) prompt… with “c” (continue)
Seeing where you are… with “l” (list)
Stepping into subroutines… with “s” (step into)
Continuing… but just to the end of the current subroutine… with “r” (return)
Assign a new value
a)
!b = "B"Set a breakpoint
a)
break linenumberb)
break functionnamec)
break filename:linenumberTemporary breakpoint
a)
tbreak linenumberConditional breakpoint
a)
break linenumber, condition
Note: All these commands should be executed from pdb
For in-depth knowledge, refer:
https://pymotw.com/2/pdb/
https://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/
Learn how to use the debugger
How to debug
To clarify, are you looking to learn debugging methods, or how to use a debugger in your editor?
If the latter, which editor are you using?
More on reddit.comNeed tips for debugging a Python project in VS Code with over 100 lines of code
Learning your IDE Debugger will change your life
Videos
I know a lot of you out there who are just getting started in python are probably using print statements to debug. While this is an easy way to debug your code there’s a lot of drawbacks when comparing it to debuggers especially in professional environments. Python has its own debugger package called pdb which uses the command line. I prefer the interactive debugger in vscode but every IDE has a debugger.
A debugger will let you mark points in code where you want to examine things further called break points. When the code reaches a break point in the debugger it will pause there allowing you to see details like variable values at that point in execution. From here you can run the code line by line as well as “step into” or out of functions. There’s also a python repl which lets you run code using all of the variables available at the breakpoint; this lets you test out different bits of code without needing to rerun everything.
If you’re still wondering why a debugger can be better than print statements in professional environments then here’s why:
You should not be committing any code with print statements. Anything that needs to be outputted to stdout should be with the logger.
Some code can take a while to run so if you’re using a debugger you don’t have to run it multiple times to test out different snippets.
In large code bases it can be difficult to trace things through; add to that layers of abstraction from object oriented programming and it can be hard sometimes to keep up. Using the debugger helps you understand what’s happening line by line.