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
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 › 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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-python-debugger
How To Use the Python Debugger | DigitalOcean
August 20, 2021 - The second line shows the current line of source code that is executed here, as pdb provides an interactive console for debugging. You can use the command help to learn its commands, and help · command to learn more about a specific command. Note that the pdb console is different than the Python interactive shell.
🌐
Visual Studio Code
code.visualstudio.com › docs › python › debugging
Python debugging in VS Code
November 3, 2021 - When you use the command, VS Code prompts you with a list of all available configurations (be sure to select the Python option): Selecting the Attach using Process ID one yields the following result: See Debugging specific app types for details on all of these configurations.
🌐
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.
🌐
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.
🌐
JetBrains
jetbrains.com › help › pycharm › debugging-your-first-python-application.html
Debug your first Python application | PyCharm Documentation
Run the script, accelerate the car once, and then brake it twice by typing the corresponding 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.
Find elsewhere
🌐
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
To use the Debug Interactive window, select Debug > Windows > Python Debug Interactive (Shift+Alt+I). The Debug Interactive window supports special meta-commands in addition to the standard REPL commands, as described in the following table:
🌐
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 - To start the Python debugger (pdb) from the command line, we need to use the -m pdb option followed by the name of the Python script you want to debug.
🌐
Python Morsels
pythonmorsels.com › debugging-with-breakpoint
breakpoint: debugging in Python - Python Morsels
November 7, 2022 - To start an interactive Python interpreter from right within your program, you can use the built-in breakpoint function to launch the Python debugger (a.k.a. PDB), and you can use the interact command within PDB to launch a regular Python REPL.
🌐
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.
🌐
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.
🌐
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 - Instead of print, you can also use the pdb specific command p, like this: (Pdb) p a 3 (Pdb_ p i 1Code language: Python (python) So what if we want to run the next line of code? There are two options: With s or step, you execute the current line. The debugger stops at the first possible occasion after that.
🌐
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).
🌐
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...
🌐
Packt
packtpub.com › en-us › learning › how-to-tutorials › debugging-and-profiling-python-scripts-tutorial
Debugging and Profiling Python Scripts [Tutorial]
We're going to look at four techniques for Python debugging: print() statement: This is the simplest way of knowing what's exactly happening so you can check what has been executed. logging: This is like a print statement but with more contextual information so you can understand it fully. pdb debugger: This is a commonly used debugging technique. The advantage of using pdb is that you can use pdb from the command line, within an interpreter, and within a program.
🌐
Netguru
netguru.com › home page › blog › essential python debugging tools you need to know
Essential Python Debugging Tools You Need to Know
September 9, 2025 - Start using PDB in a Python script by importing the pdb module and using the pdb.set_trace() statement to set breakpoints. This allows you to pause program execution and inspect variable values, providing a clear view of the program’s state at specific points. While PDB might seem basic compared to more feature-rich debuggers, it offers powerful capabilities for those who prefer or need a command-line interface.
🌐
Mostly nerdless
mostlynerdless.de › home › let’s create a python debugger together: part 1
Let's create a Python Debugger together: Part 1 - Mostly nerdlessMostly nerdless
November 10, 2023 - ➜ python3 -m pdb test.py > .../test.py(1)<module>() -> import sys (Pdb) ... (Pdb) help Documented commands (type help <topic>): ======================================== EOF c d h list q rv undisplay a cl debug help ll quit s unt alias clear disable ignore longlist r source until args commands display interact n restart step up b condition down j next return tbreak w break cont enable jump p retval u whatis bt continue exit l pp run unalias where