GivenPie
GivenPie

Reputation: 1491

Python CTRL+C to exit interpreter?

Python 2.73

Why is it on my laptop when I hit CTRL+C, I can exit the interpreter and on my desktop hitting CTRL+C will make the interpreter shoot back at me a KeyboardInterrupt message. How can I get rid of this KeyboardInterrupt and go back to exiting with CTRL+C!

On my desktop it's required to input CTRL+Z and hitting enter to exit.

I am using PowerShell on both computer. Same 64bit, one is Win7 one is Win8

Upvotes: 4

Views: 1900

Answers (2)

Stephan Wenger
Stephan Wenger

Reputation: 91

You could change the signal handler for CTRL-C to something that exits the interpreter:

import signal
import sys
signal.signal(signal.SIGINT, lambda number, frame: sys.exit())

You could probably put that code in a file to be run automatically when an interactive session starts, then set the environment variable PYTHONSTARTUP to the name of that file:

http://docs.python.org/3/using/cmdline.html?highlight=startup#envvar-PYTHONSTARTUP

Upvotes: 2

mguijarr
mguijarr

Reputation: 7900

A slightly shorter version of the previous answer:

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

SIG_DFL means default signal handling, so Python does not catch it to raise a KeyboardInterrupt exception.

Upvotes: 1

Related Questions