For my debugging needs, pdb is pretty good. However, it would be much cooler (and helpful) if I could go into ipython. Is this thing possible?
For my debugging needs, pdb is pretty good. However, it would be much cooler (and helpful) if I could go into ipython. Is this thing possible?
There is an ipdb project which embeds iPython into the standard pdb, so you can just do:
import ipdb; ipdb.set_trace()
It's installable via the usual pip install ipdb.
ipdb is pretty short, so instead of easy_installing you can also create a file ipdb.py somewhere on your Python path and paste the following into the file:
import sys
from IPython.Debugger import Pdb
from IPython.Shell import IPShell
from IPython import ipapi
shell = IPShell(argv=[''])
def set_trace():
ip = ipapi.get()
def_colors = ip.options.colors
Pdb(def_colors).set_trace(sys._getframe().f_back)
The equivalent of
import pdb; pdb.set_trace()
with IPython is something like:
from IPython.ipapi import make_session; make_session()
from IPython.Debugger import Pdb; Pdb().set_trace()
It's a bit verbose, but good to know if you don't have ipdb installed. The make_session call is required once to set up the color scheme, etc, and set_trace calls can be placed anywhere you need to break.
Normally, when I use ipython, I turn automatic debugging on with the "pdb" command inside it.
I then run my script with the "run myscript.py" command in the directory where my script is located.
If I get an exception, ipython stops the program inside the debugger. Check out the help command for the magic ipython commands (%magic)
From the IPython docs:
import IPython.ipapi
namespace = dict(
kissa = 15,
koira = 16)
IPython.ipapi.launch_new_instance(namespace)
will launch an IPython shell programmatically. Obviously the values in the namespace dict are just dummy values - it might make more sense to use locals() in practice.
Note that you have to hard-code this in; it's not going to work the way pdb does. If that's what you want, DoxaLogos' answer is probably more like what you're looking for.
This is pretty simple:
ipython some_script.py --pdb
It needs iPython installing, usually this works:
pip install ipython
I use ipython3 instead of ipython, so I know it's a recent version of Python.
This is simple to remember because you just use iPython instead of python, and add --pdb to the end.
To get IPython's REPL using terminal colors, I had to do this:
from IPython import start_ipython
start_ipython()
https://ipython.readthedocs.io/en/stable/api/generated/IPython.html#IPython.start_ipython