Take the 2-minute tour ×

I want to save all following Exceptions in a file. The reason why I need this is because the IDLE for python 3.1.1 in Ubuntu raises an Exception at calltipps, but close to fast, that it isn't readble. Also I need this for testing. The best, would be if I just call a function which saves all Exception to a file. Thank you! ;)

// edit:

i had looked first for a more general way! so that you do not have to place your whole code in a function or indentation. but now that worked wery well for me. although I would be still grateful, if you find a way!


share|improve this question
Why can't you run your script from the command line and look at the output there? –  S.Lott Nov 11 '09 at 21:46
read it again! when I try to show calltipp with STRG + SPACE the ID(L)E breaks up. I want to know what type of exception has been raised. –  Joschua Nov 11 '09 at 21:50
Please answer my question. Why aren't you running from the command line? Why are you running from IDLE? Why are you not running from the ordinary shell prompt? –  S.Lott Nov 11 '09 at 22:02
Please also answer the following questions: Are you starting IDLE with a double-click of an icon? Have you tried running IDLE from the command line to preserve IDLE's error log? –  S.Lott Nov 11 '09 at 22:10
Oh, I just opened it with command line, pressed STRG+SPACE to usally get a calltip, and this helps. The error is now printed to the terminal: Traceback (most recent call last): File "/usr/bin/idle-python3.1", line 5, in <module> main() File "/usr/lib/python3.1/idlelib/PyShell.py", line 1420, in main root.mainloop() File "/usr/lib/python3.1/tkinter/__init__.py", line 1012, in mainloop Seems to be an error in idle-python3.1 for ubuntu. Or what do you think? self.tk.mainloop(n) UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: illegal encoding –  Joschua Nov 11 '09 at 22:23
show 1 more comment

1 Answer

up vote 13 down vote accepted

If you have a convenient main() function (whatever it's called), then you can use the logging module:

import logging

def main():
    raise Exception("Hey!")

logging.basicConfig(level=logging.DEBUG, filename='/tmp/myapp.log')


logging.exception conveniently gets the current exception and puts the details in the log:

Traceback (most recent call last):
  File "C:\foo\foo.py", line 9, in <module>
  File "C:\foo\foo.py", line 4, in main
    raise Exception("Hey!")
Exception: Hey!
share|improve this answer
add comment

Your Answer


