2
name = input('name? ')
if len(name) == 0:
    print('error.\n')                            
    raise SystemExit

I receive an error when using python 3.3.2 (which is the version is school sadly) but it works fine on other versions e.g. 2.7.10 and 3.5

This is the error

This is the image of the error.

hrust
  • 734
  • 1
  • 11
  • 34
Billy
  • 202
  • 3
  • 12

4 Answers4

1

Looking at the screenshot I can see Python prompt at the bottom:

enter image description here

This means the script is run in an interactive session (IDLE on Windows I guess). I haven't found any documentation, but other users have discovered that raising SystemExit in an interactive session does print the traceback.

So you should check and ensure that you are not launching the script in an interactive session.

Old answer:

Looks like it's a bug (or a particularity) in Python 3.3.2. According to this blog post:

If nothing catches the exception, then the python interpreter catches it at the end, does not print a stack trace, and then calls exit.

I tried to raise SystemExit('asd') and the program just printed asd, so looks like it's true.

Either upgrade Python or try os._exit(1).

warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • why does the 'os._exit()' need an arguement like '1' in this case. – Billy Oct 10 '15 at 20:07
  • @Billy `1` is the process [exit](https://docs.python.org/2/library/sys.html#sys.exit) [code](https://docs.python.org/2/library/os.html#os._exit) See [also](http://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux). – warvariuc Oct 11 '15 at 06:09
  • This is just wrong/ignoring the question, you should reproduce Billy's problem rather than giving nonsense advice like "upgrade Python" – berdario Oct 11 '15 at 06:58
  • @berdario Have you reproduced? I guess you have a better solution. Let us see it. – warvariuc Oct 11 '15 at 07:07
  • @berdario I have reread my answer and seems logical. If the author used the same command to launch his script in all environments, then it's this Python version's particularity. Then nothing can be done about it, except using workarounds. Your answer is not much better, having guesses, though I haven't downvoted it. Do you think being arrogant helps you in life? – warvariuc Oct 11 '15 at 07:14
  • I have reproduced the problem, you have not. I have tried on Python 3.3.2, you have not. You have given misleading advice that will make Billy's waste time and is probably unpractical (due to 3.3.2 being the version shipped on his school's computers), I have not. I have been cautious by saying "probably", you're not: saying "it's a bug". Other than giving Billy actionable advice, I gave him a suggestion on further information to give us to receive better answers, you have not. It's not about being arrogant: it's about contributing to SO with answers that are not wrong or misleading. – berdario Oct 11 '15 at 07:36
  • Have you missed the words "Looks like"? All the world is build on opinions. I don't have access to Python 3.3.2, but it doesn't mean I am not allowed to post an answer. If you consider this a poor answer -- go on and downvote it. If the answer helped the OP and he accepted it -- SO worked. Otherwise making personal attacks like me-you -- I call this arrogance. Do what you consider it's best, but don't judge others -- Stackoverflow has other mechanisms to make the world better. – warvariuc Oct 11 '15 at 07:53
0

Not sure if this is what you want, but if you wanna exit use this:

import sys
name = raw_input('name? ')
if len(name) == 0:
    print('error.\n')
    sys.exit()         

This exits the interpreter by raising SystemExit.

MrPedru22
  • 1,324
  • 1
  • 13
  • 28
0

Why don't you use sys.exit()?

sys.exit([arg])

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

Community
  • 1
  • 1
hrust
  • 734
  • 1
  • 11
  • 34
0

You probably have an handler set for excepthook:

https://docs.python.org/3/library/sys.html#sys.excepthook

You should be able to reset it by doing

sys.excepthook = sys.__excepthook__

Nevermind, the hook works correctly for BaseException, but weirdly enough not for SystemExit (which is a subclass of BaseException).

You're probably executing your program with

python3 -i whatever.py

This gives me the same behavior you witnessed:

dario@feynman ~> python3 -i /tmp/a.py 
Traceback (most recent call last):
  File "/tmp/a.py", line 11, in <module>
     raise SystemExit()
SystemExit
>>> 

Note the >>> at the end.

Just remove the -i flag, from whatever is executing your program

Alternatively, it's bad practice, but you can also use os._exit(1)

berdario
  • 1,851
  • 18
  • 29