NBC
NBC

Reputation: 1698

How to exit Python script in Command Prompt?

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.

Upvotes: 42

Views: 147780

Answers (8)

GLARKI
GLARKI

Reputation: 81

To his problem: How to close a python server(.py) or client(.py) in the cmd window.

The answer is: cntrl + Pause or Break (Usually somewere above the left/up/down/right keys)

If that is not on your keyboard, close the cmd window and restart the server.

Eryk Sun already mentioned it, but for some reason his post isnt directly visible.

Upvotes: 1

terwxqian
terwxqian

Reputation: 659

my case(Python 3.9.6 on win10):

1 running script from the console

server: enter image description here client: enter image description here

2 stop script in Task Manager, select py.exe(not python.exe) and click the "end task" button. enter image description here enter image description here

Upvotes: 1

Sreelakshmi G
Sreelakshmi G

Reputation: 59

Use exit() or quit() if you're using Windows(I found crtl combinations absolutely didnt work and ctrl+c gave me keyboard interrupt error)

Upvotes: 1

souad988
souad988

Reputation: 1

for windows 10 python 3.9 try to type exit()

Upvotes: -2

ctrl+Z works still

..\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

Upvotes: 2

and1er
and1er

Reputation: 1101

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

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 + Z then Return.

Upvotes: 10

Ali Tohidi
Ali Tohidi

Reputation: 73

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

Upvotes: 4

Right leg
Right leg

Reputation: 16710

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

Upvotes: 64

Related Questions