R's browser() equivalent in Python
Asked Answered
H

3

19

The title says it all. When you are working R and using RStudio, its really easy and simple to debug something by dropping a browser() call anywhere in your code and seeing what goes wrong. Is there a way to do that with Python? I'm slowly getting very sick of print statement debugging.

Hildie answered 23/6, 2017 at 18:37 Comment(2)
I guess you could use an IDE and set a breakpoint. Alternatively, print(dir()) or print(locals())Zuniga
I believe you are looking for the pdbBulge
L
18

It looks like you are looking for ipdb

The basic usage is to set:

import ipdb
ipdb.set_trace()

in your code to explore; this will take you right to that part of code, so you can explore all the variables at that point.

For your specific use case: "Would it be a setting in my Console so that it Opens pdb right before something crashes" (a comment to another answer), you can use context manager: launch_ipdb_on_exception

For example:

from ipdb import launch_ipdb_on_exception

def silly():
    my_list = [1,2,3]
    for i in xrange(4):
        print my_list[i]

if __name__ == "__main__":
    with launch_ipdb_on_exception():
        silly()

Will take you to ipdb session:

      5         for i in xrange(4):
----> 6             print my_list[i]
      7

ipdb> i
3
Librate answered 23/6, 2017 at 18:40 Comment(0)
F
11

you can use python's debugger

import pdb
pdb.set_trace()

this will pause the script in debug mode

Example:

my_file=open('running_config','r')
word_count={}
special_character_count={}
import pdb
pdb.set_trace() <== The code will pause here
for config_lines in my_file.readlines():
    l=config_lines.strip()
    lines=l.upper()

Console:

> /home/samwilliams/workspace/parse_running_config/file_operations.py(6)<module>()
-> for config_lines in my_file.readlines():
(Pdb) print special_character_count
{}
(Pdb) 
Felting answered 23/6, 2017 at 18:41 Comment(3)
Would it be a setting in my Console so that it Opens pdb right before something crashes?Hildie
just write it on the script before where it crashes. it will pause in debug mode and you can just check the variables and see which variable is causing the issue.Felting
@Adam, I believe you are looking for launch_ipdb_on_exception, see my updated answer.Librate
R
1

Since python 3.7 there is now also a build-in function: breakpoint. So simply call breakpoint() in your code like you would call browser() in R.

Since it is a build-in function you do not have to first perform an import statement, like the other answers, that you might forget to remove afterwards.


Some additional details on breakpoint().

Under the hood, breakpoint calls the function sys.breakpointhook which by default would call the debugger pdb.set_trace() from the python debugger library pdb.

But you can configure the function to use any debugger you like, such as the ipython debugger ipyd.set_trace(), via the PYTHONBREAKPOINT environment variable.

Retene answered 9/5 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.