R browser () in Python
The title says it all. When you work R
and use it RStudio
, it's very easy and simple to debug something by dropping the call browser()
anywhere in your code and seeing what is going wrong. Is there a way to do this from Python? I am slowly getting very tired of debugging printing. In the meantime, there is no need to know about it. ”
source to share
It looks like you are looking for ipdb
The main use is to install:
import ipdb
ipdb.set_trace()
in your code to study; this will take you to that part of the code, so you can examine all the variables at this point.
For your specific use case, "Will there be a setting in my console to open pdb right before it crashes" (comment on another answer), you can use a 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()
Let's move on to the session ipdb
:
5 for i in xrange(4):
----> 6 print my_list[i]
7
ipdb> i
3
source to share
you can use python 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)
source to share