Python CTRL + C to exit interpreter?

Python 2.73

Why is this on my laptop, when I press CTRL + C , I can exit the interpreter, and on my desktop, hitting CTRL + C will cause the interpreter to shoot me a KeyboardInterrupt message. How can I get rid of this KeyboardInterrupt and return to completion using CTRL + C !

On my desktop, I had to type CTRL + Z and press enter to exit.

I use PowerShell on both computers. The same 64-bit, one is Win7, it is Win8

+4
source share
2 answers

You can change the signal handler for CTRL - C to what comes out of the interpreter:

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

Perhaps you could put this code in a file that will run automatically when you start an interactive session, and then set the PYTHONSTARTUP environment variable to the name of this file:

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

+2
source

A slightly shorter version of the previous answer:

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

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

+1
source

Source: https://habr.com/ru/post/1501371/


All Articles