Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run commands in IPython with debugging?

Here are my actions in IPython:

> import my_module
> import ipdb

Now, my module lacks any executable code, it only declares classes. So I want to make a statement:

> g = my_module.Graph()
> f = open('test.osm')
> g.from_osm(f)

I want to put a breakpoint inside Graph.from_osm, without editing the file. I don't want to put the latter lines into the file and to do python -m ipdb .... I just want to run commands and debug.

Is this possible?

added: I see, it's possible to

%run -d script_name

or

> import pdb
> pdb.run('statement')

but it's impossible to do ipdb.run('statement'), there's no .run in ipdb!

like image 408
culebrón Avatar asked Mar 13 '12 17:03

culebrón


People also ask

How do I use IPython for debugging?

IPython has another way to start a debugger. You don't need to modify the source code of any file as we did before. If you run the %run -d filename.py magic command, IPython will execute the filename.py file and put a breakpoint on the first line there. It's just as if you would put the import ipdb; ipdb.

How do I run a command in IPython?

If you want some code to be run at the beginning of every IPython session, the easiest way is to add Python (. py) or IPython (. ipy) scripts to your profile_default/startup/ directory. Files here will be executed as soon as the IPython shell is constructed, before any other code or scripts you have specified.

Which is used to run system commands from IPython?

IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal. The magic happens with the exclamation point: anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line.

Is debugging possible in Jupyter notebook?

Debug code in Jupyter notebooks The Jupyter Notebook Debugger tool window opens. Debugging is performed within a single code cell. However, if your code cell calls a function from any cell that has been already debugged, you can step into it. The related breakpoints will also work.


2 Answers

Since IPython 3.2.2, the %debug magic, if given an argument (a single line or a cell), executes it under debugger.

  • It breaks before executing anything, giving you a chance to set breakpoints and/or start stepping through the code.
  • And it accepts a --breakpoint argument that sets one more breakpoint (as a part of the command, it'll be saved in command history, saving you typing for repeated invocations).
like image 111
ivan_pozdeev Avatar answered Oct 22 '22 05:10

ivan_pozdeev


Perhaps the 'magic' commands %debug and / or %pdb in IPython can help you.

like image 23
bmu Avatar answered Oct 22 '22 07:10

bmu