NumPy – 7 – Errori e debugging


Copio qui continuando da qui.

Code development and data analysis always require a bit of trial and error, and IPython contains tools to streamline this process. This section will briefly cover some options for controlling Python’s exception reporting, followed by exploring tools for debugging errors in code.

Controllare le eccezioni con %xmode
Most of the time when a Python script fails, it will raise an Exception. When the interpreter hits one of these exceptions, information about the cause of the error can be found in the traceback, which can be accessed from within Python. With the %xmode magic function, IPython allows you to control the amount of information printed when the exception is raised. Consider the following code:


Using the %xmode magic function (short for Exception mode), we can change what information is printed.

%xmode takes a single argument, the mode, and there are three possibilities: Plain, Context, and Verbose. The default is Context, and gives output like that just shown before. Plain is more compact and gives less information:




This extra information can help narrow-in on why the exception is being raised. So why not use the Verbose mode all the time? As code gets complicated, this kind of traceback can get extremely long. Depending on the context, sometimes the brevity of Default mode is easier to work with.

Debugging quando non basta la traceback
IPython con il comando magigo %debug è perhaps the most convenient interface to debugging. If you call it after hitting an exception, it will automatically open an interactive debugging prompt at the point of the exception. The ipdb prompt lets you explore the current state of the stack, explore the available variables, and even run Python commands!


E questo è solo l’inizio, consente di andare oltre


This allows you to quickly find out not only what caused the error, but what function calls led up to the error.
If you’d like the debugger to launch automatically whenever an exception is raised, you can use the %pdb magic function to turn on this automatic behavior:


Finally, if you have a script that you’d like to run from the beginning in interactive mode, you can run it with the command %run -d, and use the next command to step through the lines of code interactively.

Lista (parziale) dei comandi di debugging
There are many more available commands for interactive debugging than we’ve listed here; the following table contains a description of some of the more common and useful ones:

Command     Description
list        Show the current location in the file
h(elp) 	    Show a list of commands, or find help on a specific command
q(uit) 	    Quit the debugger and the program
c(ontinue)  Quit the debugger, continue in the program
n(ext) 	    Go to the next step of the program
<enter>     Repeat the previous command
p(rint)     Print variables
s(tep) 	    Step into a subroutine
r(eturn)    Return out of a subroutine

For more information, use the help command in the debugger, or take a look at ipdb’s online documentation.

Potrei cominciare con i ricordi sulle lotte con i debugger ma poi divento noioso 😡 Invece la vita è bella, dai 😄


Posta un commento o usa questo indirizzo per il trackback.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )


Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: