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/

🌐
Visual Studio Code
code.visualstudio.com › docs › python › debugging
Python debugging in VS Code
November 3, 2021 - In the terminal, start Python with the script, for example, python3 myscript.py. You should see the "Waiting for debugger attach" message that's included in the code, and the script halts at the debugpy.wait_for_client() call.
Discussions

how do I debug a python script?
In emacs you can run `M-x pdb` which will run `pdb` debugger. Assuming you have a simple script called homework1.py , you can do M-x pdb in the prompt that follows enter "python -m pdb homework1.py " Emacs will pop a pdb shell in another window and you can run commands. There will also be a little arrow on the sides of the source code window showing you which number line you are in. Honestly, I would recommend simply doing the debugging via a terminal since you are rather new to emacs and you might be overloaded with unnecessary info at this point. More on reddit.com
🌐 r/emacs
8
0
January 11, 2024
bash - How to run python debugger with a script that takes command-line arguments? - Stack Overflow
I have a python script that takes input arguments and runs in response to the following command in terminal (bash, Mac OSX). ... Is there a good way to run the same script in debug mode without editing the code to include import pdb and pdb.set_trace()? For example, if I'm using iPython console, ... More on stackoverflow.com
🌐 stackoverflow.com
Debug a python script being called from bash
You can use pdb. Just add the line Import pdb; pdb.set_trace() and you have a debugger at that spot More on reddit.com
🌐 r/pycharm
6
3
February 19, 2023
Debug Python script that takes command line arguments?
You want a launch.json file, which you already have if running the python debugger. See the link below for more info, but here's an example { "name": "Python: startup.py", "type": "python", "request": "launch", "program": "${workspaceFolder}/startup.py", "args" : ["--port", "1593"] }, This will launch a debugger and pass args for port 1593 to the startup.py https://code.visualstudio.com/docs/python/debugging More on reddit.com
🌐 r/vscode
6
3
May 30, 2019
🌐
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:
🌐
Packt
packtpub.com › en-us › learning › how-to-tutorials › debugging-and-profiling-python-scripts-tutorial
Debugging and Profiling Python Scripts [Tutorial]
To start the debugger within a script, use set_trace(). Now, modify your pdb_example.py file as follows: import pdb class Student: def __init__(self, std): self.count = std def print_std(self): for i in range(self.count): pdb.set_trace() print(i) ...
🌐
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. The -m pdb option tells Python to run the script under the control of the pdb debugger.
Find elsewhere
🌐
jdhao's digital space
jdhao.github.io › 2019 › 01 › 16 › debug_python_in_terminal
How to Debug Python Code in Terminal · jdhao's digital space
October 16, 2021 - To execute next line of code, use n. To run the code until a break point, use c. For more usage, press h. Overall, it is quite powerful, considering that it is implemented in a terminal. In this post, I introduced three Python debuggers — pdb, ipdb and pudb.
🌐
Towards Data Science
towardsdatascience.com › home › latest › debug python scripts like a pro
Debug Python Scripts Like a Pro | Towards Data Science
March 5, 2025 - Debugging Python script in VSCode – Image by Author · Click on the debugger on the sidebar. It’s this play button with a bug on it. Create breakpoints in your code. You can do it by clicking before the line number. A red indicator will appear for every breakpoint you create. Now, start the debugger by clicking the "Run and Debug" button and selecting "Python file" in the dropdown.
🌐
Red Hat
redhat.com › en › blog › python-debugger-pdb
How to use the Python debugger (pdb)
November 20, 2025 - For the sake of argument, maybe you want the program to enter the debug mode if a module is missing (in fact, you insist there is a Celeri module). The change in the code is simple; just capture the ImportError and call the breakpoint() function (before you show the stack trace using traceback): #!/usr/bin/env python """ Script that show a basic Airflow + Celery Topology """ try: import argparse from diagrams import Cluster, Diagram from diagrams.onprem.workflow import Airflow from diagrams.onprem.queue import Celeri except ImportError: breakpoint()
🌐
Reddit
reddit.com › r/pycharm › debug a python script being called from bash
r/pycharm on Reddit: Debug a python script being called from bash
February 19, 2023 -

This is my use case. After sshing into a server, I have a bash script which creates a tunnel and then runs a python script inside that tunnel. Is is possible to debug the python script? Like adding breakpoints and stuff. Can pycharm do this? It seems to be nigh impossible in vscode

🌐
JetBrains
jetbrains.com › help › pycharm › debugging-your-first-python-application.html
Debug your first Python application | PyCharm Documentation
Run the script, accelerate the ... 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!
🌐
freeCodeCamp
freecodecamp.org › news › python-debugging-handbook
Python Debugging Handbook – How to Debug Your Python Code
January 24, 2024 - Alternatively, you can run your Python script with the -m pdb option, which automatically starts the debugger. For example: ... When your code encounters the breakpoint (either set using pdb.set_trace() or pdb.breakpoint()), it enters the ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › visualstudio › python › tutorial-working-with-python-in-visual-studio-step-04-debugging
Tutorial Step 4: Use Visual Studio Debugger - Python
Select Continue (or press F5) to run the program. When the program ends, Visual Studio stops the debugging session and returns to editing mode. You can also delete a breakpoint. Select the red dot or right-click the dot and select Delete breakpoint. This action also deletes any defined conditions. ... In some situations, such as a failure to launch the Python interpreter itself, the Python output window might close immediately after the program finishes without pausing and showing the Press any key to continue prompt.
🌐
JetBrains
jetbrains.com › help › pycharm › part-1-debugging-python-code.html
Part 1. Debugging Python Code | PyCharm Documentation
The debugger suspends the program at the first breakpoint. It means that the line with the breakpoint is not yet executed. The line becomes blue: By the way, you can enter Python commands in the Debug Console when the program is suspended:
🌐
CodeBurst
codeburst.io › how-i-use-python-debugger-to-fix-code-279f11f75866
How I Use Python Debugger to Fix Code | by Vadym Zakovinko | codeburst
July 19, 2019 - To execute the application with the debugger, use the command python -m pdb <python script>. Let me show you an example. I have this simple application that tracks my working time.
🌐
GeeksforGeeks
geeksforgeeks.org › python-debugger-python-pdb
Python Debugger – Python pdb - GeeksforGeeks
November 4, 2022 - You just have to run the following command in terminal · python -m pdb exppdb.py (put your file name instead of exppdb.py) This statement loads your source code and stops execution on the first line of code.
🌐
Medium
medium.com › @yellalena › a-guide-to-debugging-python-code-and-why-you-should-learn-it-ae30d20419b7
A Guide to Debugging Python code (and why you should learn it) | by Aliona Matveeva | Medium
August 15, 2023 - It combines all of the main functions needed for software development — code editing, building, running, testing and, finally, debugging. IDEs make debugging considerably easier so unless you’re a rigid fan of a terminal, I would recommend trying debugging in an IDE. Most IDEs operate on similar principles and it doesn’t make sense to review all of them. So I will show you a few examples in PyCharm, my preferred IDE, but you can easily do the same in any other development environment of your choice.