Python Forum
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Debugger Suggestions
#1
https://python-forum.io/Thread-MySql-Cur...7#pid61337

tbh I highly dislike that the most common way to debug an application is to print variables out. Does anyone use a debugger? Perhaps with breakpoints, variable inspections, the ability to modify variables from a breakpoint and continue with the new values, etc?
Reply
#2
What about pdb? I haven't tried it, but it comes with Python.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ok, that's actually pretty good, but super hard to figure out how to use. Let's start with a broken file to test:
def fails(test: int = 7) -> str:
    x = 42
    assert x == test


if __name__ == '__main__':
    print(fails())
Proof that it's broken:
>python spam.py
Traceback (most recent call last):
  File "spam.py", line 7, in <module>
    print(fails())
  File "spam.py", line 3, in fails
    assert x == test
AssertionError
Debug (run until error, set a breakpoint, check values at that breakpoint, and set a local variable to fix issue, then continue running):
E:\Projects\etc>python -m pdb spam.py
> e:\projects\etc\spam.py(1)<module>()
-> def fails(test: int = 7) -> str:
(Pdb) continue
Traceback (most recent call last):
  File "E:\ProgramFiles\Python37\lib\pdb.py", line 1697, in main
    pdb._runscript(mainpyfile)
  File "E:\ProgramFiles\Python37\lib\pdb.py", line 1566, in _runscript
    self.run(statement)
  File "E:\ProgramFiles\Python37\lib\bdb.py", line 585, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "e:\projects\etc\spam.py", line 1, in <module>
    def fails(test: int = 7) -> str:
  File "e:\projects\etc\spam.py", line 3, in fails
    assert x == test
AssertionError
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> e:\projects\etc\spam.py(3)fails()
-> assert x == test
(Pdb) list
  1     def fails(test: int = 7) -> str:
  2         x = 42
  3  ->     assert x == test
  4
  5
  6     if __name__ == '__main__':
  7         print(fails())
[EOF]
(Pdb) break 2
Breakpoint 1 at e:\projects\etc\spam.py:2
(Pdb) continue
Post mortem debugger finished. The spam.py will be restarted
> e:\projects\etc\spam.py(1)<module>()
-> def fails(test: int = 7) -> str:
(Pdb) continue
> e:\projects\etc\spam.py(2)fails()
-> x = 42
(Pdb) p x
*** NameError: name 'x' is not defined
(Pdb) p test
7
(Pdb) !test = 42
(Pdb) p test
42
(Pdb) continue
None
The program finished and will be restarted
> e:\projects\etc\spam.py(1)<module>()
-> def fails(test: int = 7) -> str:
(Pdb) exit
So it definitely works, but is loaded with magic incantations to work right. Maybe that's because I'm not used to command-line debuggers lol.
Reply
#4
Now you know about the debugger, it's just a matter of using it.

Before you know it, you'll be writing tests, too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Logic suggestions for comparing 2 csv's cubangt 7 1,133 Nov-09-2023, 09:54 PM
Last Post: cubangt
  Debugger azizrasul 6 1,187 Nov-02-2022, 09:46 PM
Last Post: azizrasul
  Idle debugger pops up Run.py - library? fred1232 1 1,645 Jul-27-2021, 01:01 AM
Last Post: Larz60+
  VS Code debugger using wrong Python environment topfox 0 2,509 Jun-09-2021, 10:01 AM
Last Post: topfox
  Using Spyder Full Screen Debugger ErnestTBass 0 1,474 Aug-20-2020, 08:17 PM
Last Post: ErnestTBass
  Require Some Suggestions gouravlal 2 1,889 Jul-27-2020, 06:14 AM
Last Post: gouravlal
  Debugger Disabled erictan 1 3,982 Apr-30-2020, 02:17 PM
Last Post: pyzyx3qwerty
  pydev debugger: process 3442 is connecting when I run a program with ALT+COMMAND+R Seneca260 1 2,648 Jan-06-2020, 06:57 PM
Last Post: micseydel
  Can Python Debugger (PDB) run in Python C API jibarra 0 1,850 Aug-07-2019, 05:42 PM
Last Post: jibarra
  Visual studio debugger- "Frame not in module" Yaya33 2 6,320 Nov-12-2017, 08:02 PM
Last Post: Yaya33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020