Spyder: exit isn't recognized as a command

Created on 14 Jul 2017  路  9Comments  路  Source: spyder-ide/spyder

Description of your problem

What steps will reproduce the problem?

  1. Create a simple script
  2. At the end of the script, add the command exit()
  3. Run the script, for example via the interface Run->Run

What is the expected output? What do you see instead?

I would expect the program to simply exit. Instead, I get an error "NameError: name 'exit' is not defined". This error does not occur when running directly in python.

Please provide any additional information below

This may be an iPython issue instead of a Spyder issue, I'm not sure.

sys.exit() will succeed in quitting the program, but with a warning message to use exit or quit instead, so it isn't ideal.

Versions and main components

  • Spyder Version: 3.1.4 64 bit
  • Python Version: 3.6.1
  • iPython Version: 6.1.0
  • Qt Version: 5.6.2
  • PyQt Version: 5.6
  • Operating system: Windows 7

Dependencies

Please go to the menu entry Help > Optional Dependencies (or
Help > Dependencies), press the button Copy to clipboard
and paste the contents below:

jedi >=0.9.0 : 0.10.2 (OK)
matplotlib >=1.0 : 2.0.2 (OK)
nbconvert >=4.0 : 5.2.1 (OK)
numpy >=1.7 : 1.12.1 (OK)
pandas >=0.13.1 : 0.20.2 (OK)
pep8 >=0.6 : 1.7.0 (OK)
pyflakes >=0.6.0 : 1.5.0 (OK)
pygments >=2.0 : 2.2.0 (OK)
pylint >=0.25 : 1.6.4 (OK)
qtconsole >=4.2.0: 4.3.0 (OK)
rope >=0.9.4 : 0.9.4-1 (OK)
sphinx >=0.6.6 : 1.6.2 (OK)
sympy >=0.7.3 : 1.0 (OK)

IPython Console Bug

All 9 comments

I can't reproduce this problem on Linux. @dalthviz, please take a look at this one on Windows.

Hi @eliot1785 could you provide the example script that is giving this behavior? I'm not being able to reproduce it.

Run test.py in the attached zip. I think this might be specific to calling exit() from within an object method.

Separate question: exit() works fine for me when it is NOT in an object method. However, it restarts the kernel. How can I just silently halt the program without 1) restarting the kernel, 2) causing an error message such as a stack trace? This is a common development strategy, when I want to prematurely end a program to print out variable values.

test.zip

Maybe you could use pdb and instead of exit() use import pdb; pdb.set_trace() (along with Ctrl+F12 to resume execution). Also, I think the exit() call raises an error only when you have to import the class, so this code

class Testobj:

    def example(self):
        print("printing some stuff")
        exit()
        print("printing some stuff 2")

t = Testobj()
t.example()

works for me (althoug its final behavior is to exit from the IPython Console itself).

Sure, but would you agree that this is a bug? It should work even if you import a class.

On my second point, should I raise that issue with the iPython team instead of here?

Yep I think this a bug, at least for the import part. Also, I agree with the second part, maybe in the IPython team they could give you a proper explanation of the restarting behavior. @ccordoba12 what do you think?

Run test.py in the attached zip

This seems like a bug, yes, but I can't tell for sure. However, your example doesn't work in terminal IPython either when I run it with

%run test.py

so it's not particular to Spyder.

Separate question: exit() works fine for me when it is NOT in an object method. However, it restarts the kernel. How can I just silently halt the program without 1) restarting the kernel, 2) causing an error message such as a stack trace?

I think that's not possible: exit() quits the Python interpreter, that's its purpose.

This is a common development strategy, when I want to prematurely end a program to print out variable values

Sorry, but you need to use breakpoints now, as @dalthviz said. That's the right way to do what you want in Spyder.


Since this is particular to your use case, I'm marking this issue as wishlist, which means that if you want to go through all the hassle of fixing this, we're happy to accept a pull request for it. Else, sorry to say it, but we're not going to work on it.

I think you are making the right decision since the issue I reported is with iPython, not Spyder. Sorry.

And the secondary concern I had isn't an issue per se, it is just that I don't like how the iPython interface has decided to handle terminations. That is just a design decision on iPython's part. You can actually just close this ticket entirely if you don't want to put it on the wishlist.

I am going to try your new project spyder-terminal since I think what I really want is the terminal feel.

Ok, thanks for understanding our position.

Was this page helpful?
0 / 5 - 0 ratings