196

Typing exit() exits from Python command line. Typing exit says:

Use exit() or Ctrl-Z plus Return to exit

When you type exit, you want to exit. Why does the interpreter give me the above error when it knows I am trying to exit? Why doesn't it just exit?

8
  • 2
    It might have to do with how functions are often called with () at the end... otherwise, it might (possibly) be a variable... or some sort of object... Commented Mar 16, 2012 at 0:59
  • 2
    right but the interpreter knows that i'm trying to exit and thats why prints that message. otherwise it would have printed an error message. If it knows i'm trying to exit, it can just exit. Commented Mar 16, 2012 at 1:00
  • 1
    exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works. Commented Jan 28, 2019 at 13:12
  • 1
    I think exit doesn't work with all python versions (Ctrl-Z on windows, Ctrl-D on Linux and exit() work with all python versions Commented Feb 10, 2021 at 9:08
  • I installed python on windows and ran into the same issue. "exit" does not work and even did not give me such hint. I also tried quit and [Ctrl][d] but nothing worked. Not even exit() or quit() is working. Hitting [Ctrl][z] followed by a [Return] did the job. However, with such a lousy UX I wonder why people actually end up using python - what a nightmare. All the answers trying to give arguments for this ugly behavior simply miss the point of UX. Commented Jun 9, 2022 at 16:19

14 Answers 14

78

This works for me, best way to come out of python prompt.

exit()
Sign up to request clarification or add additional context in comments.

3 Comments

I'm confused about this answer...the question itself says they already know that exit() works, and they want to know why exit without parens does not work. So this answer just repeats part of the question and doesn't actually answer it.
I kind of want to downvote this answer for not reading the question, but I suppose for most python installations this is correct; it's just that the user said this doesn't work in their case, so recommending it seems... improper.
@kettlecrab who cares about the specifics of the original question, that is for one specific person. many of us type in related keywords and end up in threads like these where these kind of answers help us.
60

In my python interpreter exit is actually a string and not a function -- 'Use Ctrl-D (i.e. EOF) to exit.'. You can check on your interpreter by entering type(exit)

In active python what is happening is that exit is a function. If you do not call the function it will print out the string representation of the object. This is the default behaviour for any object returned. It's just that the designers thought people might try to type exit to exit the interpreter, so they made the string representation of the exit function a helpful message. You can check this behaviour by typing str(exit) or even print exit.

2 Comments

Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
exit is neither a function nor a string, regardless of how you run Python. It is the same thing either way: an instance of a custom class which implements __call__ and __repr__. (Although you can avoid bringing it into existence at all with the -S flag.)
44

When you type exit in the command line, it finds the variable with that name and calls __repr__ (or __str__) on it. Usually, you'd get a result like:

<function exit at 0x00B97FB0>

But they decided to redefine that function for the exit object to display a helpful message instead. Whether or not that's a stupid behavior or not, is a subjective question, but one possible reason why it doesn't "just exit" is:

Suppose you're looking at some code in a debugger, for instance, and one of the objects references the exit function. When the debugger tries to call __repr__ on that object to display that function to you, the program suddenly stops! That would be really unexpected, and the measures to counter that might complicate things further (for instance, even if you limit that behavior to the command line, what if you try to print some object that have exit as an attribute?)

1 Comment

In this case, the real exit function is not even in scope unless you import sys, after which you would call sys.exit() to exit the interpreter.
32

With Anaconda 4.5+ and Python 3.6+ on Windows use

Ctrl+Z

or

exit()

In some cases, you might have to use

Ctrl+Break

If your computer doesn't have Break key then see here.

Comments

26

I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.

3 Comments

Seems like this method doesn't work if the script ran into an error.
Ctrl-Break works consistently in Windows. Ctrl-D or Ctrl-Z worked in Windows for Python 2.7, 3.4, 3.6. But in Python 3.7 these stopped working, and the shortest ASCII sequence that exits is Ctrl-Z Enter.
Ctrl-Break, Ctrl-D, Ctrl-Z, Ctrl-Z+Etner : none of them worked for me to terminate a simple python script from keep running on a shell's terminal. only when I close the shell itself. Why is it? I am running pyhton 3.7
22

This message is the __str__ attribute of exit

look at these examples :

1

>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit

2

>>> exit.__str__()
'Use exit() or Ctrl-D (i.e. EOF) to exit'

3

>>> getattr(exit, '__str__')()
'Use exit() or Ctrl-D (i.e. EOF) to exit'

Comments

12

Because the interpreter is not a shell where you provide commands, it's - well - an interpreter. The things that you give to it are Python code.

The syntax of Python is such that exit, by itself, cannot possibly be anything other than a name for an object. Simply referring to an object can't actually do anything (except what the read-eval-print loop normally does; i.e. display a string representation of the object).

1 Comment

11 years later: theoretically, since the interpreter prints the string representation of the results that are calculated, the necessary logic could be put into the __repr__ of the class that exit instantiates. However, this would cause surprising and inconsistent behaviour, especially when comparing what happens in a script vs. in the interpreter.
10

You can fix that.

Link PYTHONSTARTUP to a python file with the following

# Make exit work as expected
type(exit).__repr__ = type(exit).__call__

How does this work?

The python command line is a read-evaluate-print-loop, that is when you type text it will read that text, evaluate it, and eventually print the result.

When you type exit() it evaluates to a callable object of type site.Quitter and calls its __call__ function which exits the system. When you type exit it evaluates to the same callable object, without calling it the object is printed which in turn calls __repr__ on the object.

We can take advantage of this by linking __repr__ to __call__ and thus get the expected behavior of exiting the system even when we type exit without parentheses.

2 Comments

They took the effort to make exit print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.
And/or (as mentioned above) just use ctrl+d (cntrl+z on windows)
5

To exit from Python terminal, simply just do:

exit()

Please pay attention it's a function which called as most user mix it with exit without calling, but new Pyhton terminal show a message...

or as a shortcut, press:

Ctrl + D

on your keyboard...

ctrl + D

1 Comment

Everything you said has been covered in the answers above already.
4

Very old post, but maybe useful to update it, for python 3.12 console command:

import sys
sys.exit()

Alternative, still working:

CTRL+Z and Enter

Comments

1

Starting from Python 3.13(will be released on October 7, 2024), with the new interactive shell, You can exit the shell by typing exit or quit. Adding call parentheses after those commands is not required.

Comments

0

If you stuck in python command line and none of above solutions worked for you, try exit(2)

Comments

-1

"exit" is a valid variable name that can be used in your Python program. You wouldn't want to exit the interpreter when you're just trying to see the value of that variable.

2 Comments

Except if you assign to exit, you override this symbol, and exit() will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.
While the other comment under this answer has a point, the answer shouldn't be down-voted, because it is actually an answer to the question, and Chad knows what he's talking about, unlike the other popular cruft that don't even answer the OPs question.
-3

This UX in python:

$ python
Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
>>> ^D
  File "<stdin>", line 1
    ♦
    ^
SyntaxError: invalid syntax
>>> exit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'exit' is not defined
>>> quit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'quit' is not defined
>>> exit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'exit' is not defined
>>> quit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'quit' is not defined

1 Comment

As mentioned several times above, ctrl+z for windows and ctrl+d for unix.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.