[IPython-user] how to debug a temp file
Fernando.Perez at colorado.edu
Wed May 18 19:39:28 CDT 2005
danny shevitz wrote:
> So I can't figure out how to debug an interactive session elegantly.
> I have an ipython session where I have been doing some useful stuff. I
> have been using magic edit a lot. The useful script now resides in a
> temp file. But alas, there is a bug. I need to debug. I suspect the bug
> is in a function I call, so I want to break there.
> I'm stuck how to do this elegantly in ipython. Here are the options as
> I know them.
> 1) Put 1/0 in the temp file. Run with Pdb on. Unfortunately, this
> doesn't work because I want to break at the beginning of the routine,
> and if I throw an exception, I can't continue past it (at least I don't
> know how)
> 2) Save the temp file to a permanent file and then use magic run -d,
> but I don't really want to save the file explicitly. This works fine,
> but I'm in a version controlled environment and then I have junk files
> that aren't versioned... So I don't like this.
> 3) use _n to get the output of the history and then explicitly do a pdb
> import, pdb.run(_n). This works, but isn't great since the top level is
> a single string and so setting breaks is impossible.
> So that's it. I'm stuck for an elegant way. My suggestion is to add a
> new flag to the magic run (-t, for temp) which runs the last tempfile.
> This way hopefully, you could use a syntax like run -d -t and this
> would do exactly what I want. In principle you could add history
> numbers for which tempfile to debug, but using ed _n will make a new
> one with the desired state. In fact you could even have run -d default
> to the last tempfile without an explicit filename given. I don't know
> if ipython tracks tempfile names though.
> I guess in summary, ipython has magic for profiling and timing
> interactive code, but debugging is only accessible via explicity pdb
> calls or else the run -d but that requires a named file, which
> completely abuses the notion of ipython being an interactive
> environment. Any comments or suggestions.
I'm sorry, but I'm not going to add a special flag to %run just so you don't
have to save a file. The generic usage case for %edit works well, since on
every recall of %edit, the editor starts, people work, and upon exit ipython
re-executes their code. It's just that because you are:
1. using a permanently live instance of an editor to work on a tmp file
2. don't want to save that file anywhere in your file system because of your
version control setup
that you are finding it a bit constraining. Sorry, but your usage case is too
special to warrant a special flag in the general codebase, which would not be
useful in almost any normal situation.
Yet, if you want to have the behavior you desire, all you need to do is write
a personal magic function (say %trun) which picks out the name of the last
temp file for you (hint, they are stored in magics in self.shell.tempfiles),
and then calls %run with the proper flags.
That's a much cleaner solution for your personal needs, without polluting the
More information about the IPython-user