添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to manually abort/stop (I call this KeyboardInterrupt) the execution of a Python3 script, using Spyder??

When I click run file or run current cell , nothing can stop the execution of a script until it is finished.

Python 3.7.0 64-bit, Qt 5.9.6, PyQt5 5.9.2, Spyder 3.3.4, Anaconda 3 64bit, Windows 10 64bit

Ctrl + C doesn't work, Ctr+X doesn't work, The red "stop the current command" button in IPython console doesn't work.
The only way I can stop a running script is by menu Consoles> Restart kernel.

This example code shows that KeyboardInterrupt doesn't work in Spyder, in none of the 3 sections below.

@author: xynxnex'''
def hello():
    print('Hello Spyder '+str(nr))
import time
nr = 0
while nr < 42:
    nr +=1
    hello()
    time.sleep(1)    
#%% loop in function
def hello(n):
    nr = 0
    while nr < n: #n is a local variable, thus not shown in Variable explorer
        nr +=1 #nr is a local variable, thus not shown in Variable explorer
        print('Hello Spyder '+str(nr))
        time.sleep(1)
    return n
import time
x = hello(42) #
#%% just a simple loop
import time
nr = 0
while nr < 42:
    nr +=1
    print('Hello Spyder '+str(nr))
    time.sleep(1)

No errors, but the loop just keeps on executing until it is finished.

Ctrl c (not capital c) produced the keyboard interrupt for me. Spyder 3.3.6 Python 3.6.8 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10

#%% just a simple loop
    import time
    nr = 0
    while nr < 42:
        nr +=1
        print('Hello Spyder '+str(nr))
        time.sleep(1)
Hello Spyder 1
Hello Spyder 2
Traceback (most recent call last):
  File "<ipython-input-23-b2c4320912d9>", line 7, in <module>
    time.sleep(1)
KeyboardInterrupt
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.