Exiting from python Command Line
Solution 1
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
.
Solution 2
This works for me, best way to come out of python prompt.
exit()
Solution 3
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?)
Solution 4
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
Solution 5
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.
Related videos on Youtube
Ank
Updated on March 16, 2021Comments
-
Ank about 3 years
To exit from Python command line, I have to type exit(). If I type exit, it says
Use exit() or Ctrl-Z plus Return to exit
Usually when you type
exit
, you would want to exit the program. Why does the interpreter give me the above error when it knows I am trying to exit the command line? Why doesn't it just exit? I know it doesn't matter and its a silly question but I am curious.-
summea about 12 yearsIt 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... -
Ank about 12 yearsright 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.
-
Paulo Carvalho over 5 yearsexit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
-
gelonida over 3 yearsI think
exit
doesn't work with all python versions (Ctrl-Z
on windows,Ctrl-D
on Linux andexit()
work with all python versions
-
-
Karl Knechtel about 12 yearsCtrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
-
Dunes about 12 years@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
-
Michael Dillon about 12 yearsIn 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. -
blaylockbk over 7 yearsSeems like this method doesn't work if the script ran into an error.
-
Przemek D about 6 yearsThey 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. -
Przemek D over 5 yearsExcept if you assign to
exit
, you override this symbol, andexit()
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. -
Przemek D over 5 yearsEverything you said has been covered in the answers above already.
-
Bob Stein about 5 years
Ctrl-Break
works consistently in Windows.Ctrl-D
orCtrl-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 isCtrl-Z
Enter
. -
Ben Lee over 4 yearsI'm confused about this answer...the question itself says they already know that
exit()
works, and they want to know whyexit
without parens does not work. So this answer just repeats part of the question and doesn't actually answer it. -
person27 over 4 yearsI 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.
-
GyRo almost 2 yearsCtrl-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