How to make alias "q" to quit python shell

Python 3.9 on Windows 10. I’m still new to Python.

How can I make an alias or program or something to quit the python shell by typing “q” and ENTER? “q” would call the “quit()” function.

It would just make it that much easier to quit Python.

p.s. I’m really enjoying Python and beginning to write small programs with it. Thanks to all of your help! :slight_smile:

This is certainly possible, but it’s almost certainly going to frustrate you :slight_smile: The key here is to note that, whenever you type something like that, Python will show you the representation of the thing you looked at. So we make that representation exit the interpreter.

class InstaQuit:
    def __repr__(self):
        raise SystemExit

q = InstaQuit()

But now, any time ANYTHING looks at that - such as globals() - you’ll quit out. A more sophisticated solution would be to change sys.displayhook to special-case your quitter object. but that’s a bit more effort.

2 Likes

An easier way is to press Ctrl + Z, ENTER.

1 Like

On Windows, yes. On Linux it should be Ctrl-D. (No enter press required, but it does need to be at the start of a line.)

3 Likes

D like down?

D like the key between S and F on a standard QWERTY layout.