用于在 Powershell 中退出 Python 的 Ctrl-C 现在不起作用

Ctrl-C for quitting Python in Powershell now not working

Python 在 Powershell/Command 提示符下使用 Ctrl-C 时无法退出,而是给出一个 "KeyboardInterrupt" 字符串。

最近我重新安装了Windows 10。在重新安装之前Ctrl-C 退出python (3.5/2.7) 很好,没有输出。

有谁知道为什么会出现这种情况?是否只是简单的设置?

我能想到的唯一区别是我现在使用的是 python 3.6。 Ctrl-D 在 Bash on Ubuntu on Windows 中工作,Ctrl-C 在激活的 anaconda python2 环境中工作正常以退出 python。

当您按下 Control C 时,会引发 KeyboardInterrupt 异常。如果它没有停止代码,最好的办法是在你的代码中添加一个 try 语句来捕获 KeyboardInterrupt

try:
    ....

except KeyboardInterrupt:
    exit()

代码只是在我的脑海中,如果有什么不对的地方,我们深表歉意。

编辑:Ctrl Break 或在某些键盘上 Ctrl Pause 显然会立即停止 python 代码。

这是最近出现在 Windows 10 Insider build 15002 中的错误。

解决方法是将映射键从 Ctrl C 更改为 Ctrl K

如果您不熟悉如何执行此操作,您可以查看或查看 stty -a

您可以在每个 bash 会话上 运行 此命令,它将您的终止映射到 Ctrl + K

stty intr \^k

作为 TEMP 解决方案,您可以将其包含在 Bashrc 中,以便在每个新会话中执行它

此错误已在 Github#1569

上报告

在 Windows-7 上,在 Spyder 中使用 numpy 运行,在 Python 3.+ 上,按下键盘上角的 Esc 按钮似乎对我有用 它打破了无限......:交互式脚本中的错误语法

您可以输入

CTRL + Z, 

然后按 ENTER 退出 powershell python。

Powershell Screenshot

When I used to press Ctrl-C, python was quit normally without any output.

我真的不记得什么时候可以用Ctrl + C退出python。在我的记忆中,Ctrl + C总是给我KeyboardInterrupt.


我也找不到解释为什么 Ctrl + C 无法退出 shell 中的 python 的答案。仅供参考,Ctrl + DCtrl + \ 可以出于不同的原因退出 python。请在此处查看@Gilles 的回答。 https://superuser.com/questions/169051/whats-the-difference-between-c-and-d-for-unix-mac-os-x-terminal


但我认为如何处理Ctrl + C只是程序的决定。是的,我认为这完全取决于程序如何处理它。请参阅下面我写的示例。

test.c

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static void ctrlC_Handler(int sig){
    if(sig == SIGINT){
        printf("(:KeyboardInterrupt:)\n");
        printf(">>>");
        signal(SIGINT, ctrlC_Handler);
    }
}

static void welcome(){
    printf("Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)\n");
    printf("[GCC 7.3.0] on linux2\n");
    printf("Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n");
    printf(">>>");
    fflush(stdout);
}

int main(){
    welcome();
    signal(SIGINT, ctrlC_Handler);

    for(;;){
        pause();
    }

    return 0;
}

gcc test.c编译并执行./a.out。尝试按 Ctrl + C,你至少可以看到当你输入 Ctrl + C.

时它模仿了 python 程序的行为(输出)

所以,在我看来,这没什么特别的,但是 human/programmer/author 决定如何处理它。

在我的例子中,我发现 right ctrl + c 可以在 anaconda3 powershell 中使用 - 所以不需要重新映射 - 我在 Windows 10。

我在使用 Windows 10 Pro Build 18363 和 Python 3.8.1 时遇到了这个问题。我正在 运行 运行一些 python 脚本并且无法使用 CTRL + C 停止一些脚本,但是 CTRL + BREAK 每次都能正常工作。 The Windows Docs 有话要说:

The CTRL+C and CTRL+BREAK key combinations receive special handling by console processes. By default, when a console window has the keyboard focus, CTRL+C or CTRL+BREAK is treated as a signal (SIGINT or SIGBREAK) and not as keyboard input...

CTRL+BREAK is always treated as a signal, but an application can change the default CTRL+C behavior in two ways that prevent the handler functions from being called:

  1. The SetConsoleMode function can disable the ENABLE_PROCESSED_INPUT input mode for a console's input buffer, so CTRL+C is reported as keyboard input rather than as a signal.
  2. When SetConsoleCtrlHandler is called with NULL and TRUE values for its parameters, the calling process ignores CTRL+C signals. Normal CTRL+C processing is restored by calling SetConsoleCtrlHandler with NULL and FALSE values. This attribute of ignoring or not ignoring CTRL+C signals is inherited by child processes, but it can be enabled or disabled by any process without affecting existing processes.

因此,CTRL + C 似乎是一个 SIGINT,它的动作可以被您正在 运行ning 的程序修改。似乎 Windows 上的 Python 的编码方式使得 CTRL + C 被处理为键盘输入,而不是我们期望的 SIGINT。对我来说幸运的是,我的键盘上有 CTRL + BREAK 键,而且每次都有效。

对于键盘上没有 BREAK 的用户,您可以使用 Windows 屏幕虚拟键盘。

  1. win key + r打开运行应用程序。
  2. 输入osk然后按确定
  3. 在虚拟键盘上,按 ctrl + ScrLk 这应该会终止程序。

如果 ctrl + ScrLk 在虚拟键盘上不起作用,您可以尝试此 stack thread 的一些其他方法。

神奇的效果

如果您有带 Fn 键的笔记本电脑,请点击:

Ctrl + Fn + S

来源: https://www.dell.com/community/Laptops-General-Read-Only/break-key-alternative/td-p/3826467