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 breakpoint
  • c: continue debugging until you hit a breakpoint
  • s: step through the code
  • n: to go to next line of code
  • l: list source code for the current file (default: 11 lines including the line being executed)
  • u: navigate up a stack frame
  • d: navigate down a stack frame
  • p: 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 Overflow
🌐
Python
docs.python.org › 3 › library › pdb.html
pdb — The Python Debugger
The debugger’s prompt is (Pdb), which is the indicator that you are in debug mode: > ...(2)double() -> breakpoint() (Pdb) p x 3 (Pdb) continue 3 * 2 is 6 · Changed in version 3.3: Tab-completion via the readline module is available for commands and command arguments, e.g. the current global and local names are offered as arguments of the p command. You can also invoke pdb from the command line to debug other scripts. For example: python -m pdb [-c command] (-m module | -p pid | pyfile) [args ...]
Top answer
1 of 15
409

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 breakpoint
  • c: continue debugging until you hit a breakpoint
  • s: step through the code
  • n: to go to next line of code
  • l: list source code for the current file (default: 11 lines including the line being executed)
  • u: navigate up a stack frame
  • d: navigate down a stack frame
  • p: 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.

2 of 15
87

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

  1. Execute the next statement… with “n” (next)

  2. Repeating the last debugging command… with ENTER

  3. Quitting it all… with “q” (quit)

  4. Printing the value of variables… with “p” (print)

    a) p a

  5. Turning off the (Pdb) prompt… with “c” (continue)

  6. Seeing where you are… with “l” (list)

  7. Stepping into subroutines… with “s” (step into)

  8. Continuing… but just to the end of the current subroutine… with “r” (return)

  9. Assign a new value

    a) !b = "B"

  10. Set a breakpoint

    a) break linenumber

    b) break functionname

    c) break filename:linenumber

  11. Temporary breakpoint

    a) tbreak linenumber

  12. Conditional 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/

🌐
Python
docs.python.org › 3 › library › debug.html
Debugging and Profiling — Python 3.14.3 documentation
These libraries help you with Python development: the debugger enables you to step through code, analyze stack frames and set breakpoints etc., and the profilers run code and give you a detailed br...
🌐
Real Python
realpython.com › python-debugging-pdb
Python Debugging With Pdb – Real Python
May 19, 2023 - You can also break into the debugger, without modifying the source and using pdb.set_trace() or breakpoint(), by running Python directly from the command-line and passing the option -m pdb. If your application accepts command-line arguments, pass them as you normally would after the filename.
🌐
Visual Studio Code
code.visualstudio.com › docs › python › debugging
Python debugging in VS Code
November 3, 2021 - A configuration menu will open from the Command Palette allowing you to choose the type of debug configuration you want to use for our Python project file. If you want to debug a single Python script, select Python File in the Select a debug configuration menu that appears. Note: Starting a debugging session through the Debug Panel, F5, or Run > Start Debugging when no configuration exists will also bring up the debug configuration menu, but will not create a launch.json file.
🌐
freeCodeCamp
freecodecamp.org › news › debugging-in-python-using-pdb
How to Debug Your Python Code with the Python Debugger (pdb)
September 27, 2022 - What if you want to read the raw database query? In that case you can call pdb from inside the Python function. To break into the pdb debugger, you need to call import pdb; pdb.set_trace() inside your function.
🌐
Microsoft Learn
learn.microsoft.com › en-us › visualstudio › python › debugging-python-in-visual-studio
Debug Python code, set breakpoints, inspect code - Visual Studio (Windows) | Microsoft Learn
The basic debugging workflow involves settings breakpoints, stepping through code, inspecting values, and handling exceptions. You can start a debugging session by selecting Debug > Start Debugging or use the F5 keyboard shortcut.
🌐
Python Tips
book.pythontips.com › en › latest › debugging.html
2. Debugging — Python Tips 0.1 documentation
These are just a few commands. pdb also supports post mortem. It is also a really handy function. I would highly suggest you to look at the official documentation and learn more about it. ... It might seem unintuitive to use pdb.set_trace() if you are new to this. Fortunately, if you are using Python 3.7+ then you can simply use the breakpoint() [built-in function](https://docs.python.org/3/library/functions.html#breakpoint).
Find elsewhere
🌐
DataFlair
data-flair.training › blogs › python-debugger
Python Debugger with Examples - Functions & Command Prompt - DataFlair
July 16, 2025 - This command creates an alias called name that executes command. ... Here, we created an alias ‘y’ to print ‘x’. This deletes the alias name. ... This executes a single line of statement in the current stack frame’s context. ... This restarts the debugged Python program.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-python-debugger
How To Use the Python Debugger | DigitalOcean
August 20, 2021 - The jump statement with the Python debugger allows you to change the execution flow while debugging a program to see whether flow control can be modified to different purposes or to better understand what issues are arising in your code. Here is a table of useful pdb commands along with their short forms to keep in mind while working with the Python debugger.
🌐
Visual Studio Code
code.visualstudio.com › docs › python › python-quick-start
Quick Start Guide for Python in VS Code
November 3, 2021 - From the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), select the Python: Start REPL command to open a REPL terminal for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time. The debugger is a helpful tool that allows you to inspect the flow of your code execution and more easily identify errors, as well as explore how your variables and data change as your program is run.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-debugger-python-pdb
Python Debugger – Python pdb - GeeksforGeeks
November 4, 2022 - To start debugging within the program just insert import pdb, pdb.set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace(). With python 3.7 and later versions, there is a built-in function called breakpoint() which works in the same manner.
🌐
JetBrains
jetbrains.com › help › pycharm › debugging-your-first-python-application.html
Debug your first Python application | PyCharm Documentation
October 11, 2024 - Run the script, accelerate the ... commands in the Run tool window: Now press o followed by Enter to show the car's odometer: The script is telling us that the car has travelled 0 kilometers! It's an unexpected result, because we've pushed the accelerator once, so the car should have covered some distance. Let's debug the code to find out the reason for that. To start debugging, you have ...
🌐
Python Land
python.land › home › tips & tricks › python debugger: effortlessly improve your debug skills
Python Debugger: Effortlessly Improve Your Debug Skills • Python Land Tips & Tricks
April 9, 2022 - Also takes an argument, e.g. h c tells you what the c command does · c (continue): start running until the next breakpoint · l (list): list the source code around the current line · p: print the result of the following expression. You can use this instead of calling print() pp: pretty print, useful for printing data structures like dictionaries ... For the complete documentation, head over to the Python debugger page, where all the commands are listed.
🌐
InterWorks
interworks.com › home › get to know pdb, the python debugger
Get to Know pdb, the Python Debugger - InterWorks
January 27, 2022 - Wherever you put this line in your code, the Python interpreter will stop and present an interactive debugger command prompt. From here, you can run any arbitrary Python code, or issue pdb commands to step through or even into the code by using next and step, respectively.
🌐
W3Resource
w3resource.com › python-interview › how-do-you-start-the-python-debugger-pdb-from-the-command-line.php
Initiating Python debugger (pdb) from command line
August 12, 2023 - At this point, you can use various 'pdb' commands to interactively debug your Python code. These commands include stepping through the code, inspecting variables, setting breakpoints, and more. Remember to include the 'pdb.set_trace()' statement in your script at the location where you want the debugger to pause.
🌐
Ugoproto
ugoproto.github.io › ugo_py_doc › pdf › Python-Debugger-Cheatsheet.pdf pdf
Python Debugger Cheatsheet Getting started
Python Debugger Cheatsheet · Getting started · import pdb;pdb.set_trace() start pdb from within a script · python -m pdb <file.py> start pdb from the commandline · Basics · h(elp) print available commands · h(elp) command print help about command · q(quit) quit debugger ·
🌐
Python 101
python101.pythonlibrary.org › chapter24_debugging.html
Chapter 24 - The Python Debugger — Python 101 1.0 documentation
This preserves the debugger’s state (such as breakpoints) and can be more useful than having the debugger stop. Sometimes you’ll need to go through the code several times to understand what’s wrong with it. Let’s dig a little deeper and learn how to step through the code. If you want to step through your code one line at a time, then you can use the step (or simply “s”) command. Here’s a session for your viewing pleasure: C:\Users\mike>cd c:\py101 c:\py101>python -m pdb debug_test.py > c:\py101\debug_test.py(4)<module>() -> def doubler(a): (Pdb) step > c:\py101\debug_test.py(11)<
🌐
Python Module of the Week
pymotw.com › 2 › pdb
pdb – Interactive Debugger - Python Module of the Week
July 3, 2010 - The debugger monitors your program, and when it reaches the location described by a breakpoint the program is paused before the line is executed. There are several options to the break command used for setting break points. You can specify the line number, file, and function where processing should pause. To set a breakpoint on a specific line of the current file, use break lineno: $ python -m pdb pdb_break.py > .../pdb_break.py(7)<module>() -> def calc(i, n): (Pdb) break 11 Breakpoint 1 at .../pdb_break.py:11 (Pdb) continue i = 0 j = 0 i = 1 j = 5 > .../pdb_break.py(11)calc() -> print 'Positive!'