Exiting from python Command Line
Question:
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.
Answers:
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
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
.
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'
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?)
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).
This works for me, best way to come out of python prompt.
exit()
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.
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
“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.
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.
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
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.
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
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
.
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'
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?)
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).
This works for me, best way to come out of python prompt.
exit()
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.
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
“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.
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.
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