Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit Python script in Command Prompt?

Tags:

python

windows

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.

But when I do that on my computer it tells me "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?

Thanks.

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

like image 239
NBC Avatar asked Jan 07 '17 18:01

NBC


People also ask

How do you exit a Python script code?

To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program from running. It is the most reliable, cross-platform way of stopping code execution.


1 Answers

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

As you mentioned, ctrl+C does 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.

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

  • ctrl+C
  • ctrl+D
  • ctrl+Z then Return

In addition, the following should work with any terminal:

  • exit() then Return
  • quit() then Return

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

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

like image 75
Right leg Avatar answered Sep 23 '22 02:09

Right leg