--- BEGIN OF SESSION TRANSCRIPT --- $ python Python version: 2.2.2 (#2, Feb 5 2003, 10:40:08) [GCC 3.2.1 (Mandrake Linux 9.1 3.2.1-5mdk)] on linux-i386 Type "help", "copyright", "credits" or "license" for more information. >>> $ cat exit.py exit(1) $ python exit.py Traceback (most recent call last): File "exit.py", line 1, in ? exit(1); TypeError: 'str' object is not callable $ cat exitstr.py exit("Fatal error") $ python exitstr.py Traceback (most recent call last): File "exitstr.py", line 1, in ? exit("Fatal error"); TypeError: 'str' object is not callable --- END OF SESSION TRANSCRIPT --- The first "python ..." command shall output nothing and return 1 as the result. The second "python ..." command shall output "Fatal error" to stderr and return 1 as the result. No exception shall be raised there.
Logged In: YES user_id=31435 It appears that you want to use the os.exit() function. If so, you must import it from os. The builtin exit (which you're using) is indeed just a string, which displays a message to interactive users telling them the correct way to end an interpreter session. This varies across platforms. For example, here under Cygwin: $ python Python 2.2.2 (#1, Dec 31 2002, 12:24:34) [GCC 3.2 20020927 (prerelease)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> exit 'Use Ctrl-D (i.e. EOF) to exit.' >>> "'str' object is not callable" is a perfectly appropriate exception if you try to (as you are trying to do) *call* a string with an argument.
Logged In: YES user_id=31435 Oops! Everywhere I wrote "os" read "sys" instead. The sys.exit() function does what you want here. There is no exit function in the os module (although there is a lower-level os._exit() function).
Logged In: YES user_id=688090 Thanks. Now I looked at the example I was looking in my book before and I did realized that they used unobtrusive "from sys import exit" line at the *bottom* of the example script. When you are working with compiled languages such as C for so long time, the way you reads programs will be affected so that you will get *really* confused if you see such a piece of example code. So when I looked at the code and saw "exit()" call, I thought that the exit() is a builtin function (it was very long example and the exit() call was on different page as the "from sys import exit" line, so I didn't notice the import at first look) and when I got that exception, I began to think about a bug being in Python (after I really carefully compared to the example I was looking on ;) ). P.S. I'm closing this "bug" when I realized that there is no such a bug in Python. P.SS. I suggest you to not define the builtin "exit" when you are not in the interactive mode (i.e. running a script) to avoid further such confusions.