Python:如何在命令提示符中退出 Python 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41524734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:10:35  来源:igfitidea点击:

Python: How to exit Python script in Command Prompt?

pythonwindows

提问by NBC

On previous computers, when I would try to exit a python script on the Windows command prompt, all you need to do is press ctrl+c.

在以前的计算机上,当我尝试在 Windows 命令提示符下退出 python 脚本时,您需要做的就是按ctrl+ c

But when I do that on my computer it tells me "KeyboardInterrupt":

但是当我在我的电脑上这样做时,它告诉我“KeyboardInterrupt”:

C:\Windows\System32
>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> [I press ctrl+c]
KeyboardInterrupt
>>>

So how do I fix this so I can exit the Python script?

那么我该如何解决这个问题,以便我可以退出 Python 脚本呢?

Thanks.

谢谢。

Edit:
ctrl+zworks, but i need to enter it as code. Was hoping for a quick and easy way to just exit the script, but oh well.

编辑
ctrl+z有效,但我需要将其作为代码输入。希望有一种快速简便的方法来退出脚本,但是很好。

回答by Right leg

It indeed depends on the OS, and probably on the version of Python you are using.

这确实取决于操作系统,也可能取决于您使用的 Python 版本。

As you mentioned, ctrl+Cdoes not work on your Windows 10 with Python 3.6, but it does work on my Windows 10 with Python 3.4. Therefore, you really need to try and see what works for you.

正如您所提到的,ctrl+C不适用于使用 Python 3.6 的 Windows 10,但它适用于使用 Python 3.4 的 Windows 10。因此,你真的需要尝试看看什么对你有用。

Try the following commands, and keep the one that works:

尝试以下命令,并保留有效的命令:

  • ctrl+C
  • ctrl+D
  • ctrl+Zthen Return
  • ctrl+C
  • ctrl+D
  • ctrl+Z然后Return

In addition, the following should work with any terminal:

此外,以下内容适用于任何终端:

  • exit()then Return
  • quit()then Return
  • exit()然后 Return
  • quit()然后 Return

Trivia: if you type quitand hit Return, the console tells you, at least for Python 3.4:

琐事:如果您输入quit并点击Return,控制台会告诉您,至少对于 Python 3.4:

Use quit() or Ctrl-Z plus Return to exit

使用 quit() 或 Ctrl-Z 加 Return 退出

回答by and1er

For an embedded python (e.g. python-3.6.8-embed-win32) quit()command does not work

对于嵌入式python(例如python-3.6.8-embed-win32quit()命令不起作用

Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:17) [MSC v.1916 32 bit (Intel)] on win32
>>> quit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'quit' is not defined

The only way that works is: CTRL+ Zthen Return.

唯一有效的方法是:CTRL+ Zthen Return

回答by Ali Tohidi

You could simply type "quit()" and its done!
CTRL+ Cwill interrupt a running script; but you only want to quit the interpreter. So quit() function will work for you.

你可以简单地输入“quit()”就完成了!
CTRL+C会中断正在运行的脚本;但你只想退出解释器。所以 quit() 函数对你有用。

回答by Vijayasankar Balasubramanian

ctrl+Z works still

ctrl+Z 仍然有效

..\AppData\Local\Programs\Python\Python37>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
>>>
>>> (1+4)*2
10
>>> ^Z