Running python code In pycharm, KeyboardInterrupt is not working - python

I want KeyboardInterrupt part run by clicking red button(stop) in Pycharm, but it doesn't work.
I tried it in Anaconda3 prompt, and KeyboardInterrupt worked.
However, when I try it in Pycharm by clicking red button, it doesn't work.
import time
try:
while True:
time.sleep(1)
print(3)
except KeyboardInterrupt:
print(5)
'Process finished with exit code -1' pops up and nothing happened.

In this case, you use try-except. So when you stop your program it print 5 in your pycharm console.

Related

How to stop execution of script, except without exiting interactive session

I like to paste hunks of code into an ipython window during development and debug. I also have a situation where I want to stop execution if a condition happens:
if condition:
<stop running>
Solutions like sys.exit() appear to exit all the way out of ipython back to a terminal prompt. Is there another way? At the moment I do this:
if condition:
fjklsd;
Which gives an error and returns to the ipython prompt, but is pretty ugly too.
You can raise this exception raise KeyboardInterrupt

Can't catch Ctrl+C in input() with Spyder 3.3

How do I capture Ctrl+C in line and exit the program as user enters it. I am using Python 3.7.3 on IPython 7.4.0 on spyder 3.3.3 on windows 64 bit machine with 8 GB RAM.
The most important thing is that when I run this program on windows powershell it works, this is happening only with spyder.
I tried using threads, KeyboardInterrupt, etc. but nothing works. I also saw lots of post regarding this but none is useful. Thing is that everytime python fails to capture Ctrl + C.
while True:
line = input()
if ('line contains Ctrl+C')
break
print(line)
print("Exiting")
I expect that after pressing Ctrl + C , program will print "Exiting" and stop execution.
The main thing there is what happens when you press Ctrl+C. It just stops. That being said, you might be able to except KeyboardInterrupt. Have you tried this?
import sys
try:
#Code Stuff Goes Here
except KeyboardInterrupt:
print(Exiting...)
sys.exit()

Why While True loop does not interrupt by `KeyboardInterrupt` on Pycharm?

I want to interrupt my while True loop in below code but it does not work:
try:
while True:
print(1)
except KeyboardInterrupt:
print('interrupted!')
exit()
I am running this code in PyCharm. Why isn't Ctrl+C triggering KeyboardInterrupt?
In PyCharm Ctrl+C is bound to "copy". Try Ctrl+F2 instead.
Have you tried pressing Ctrl+C, because that's the correct way of causing KeyboardInterrupt
As #Chris mentioned it won't work for pycharm. Try using Ctrl+F2 for that

how to exit python execfile() with out exiting whole script

I have a code which runs mutile python codes such as below:
execfile("1.py")
execfile("2.py")
execfile("3.py")
however occasionally one of the above codes as an error, i put exit('error') in the code to cancell if there is an error. However i want the rest of the code to run and exit('error') exits the whole code, not just the execfile. How do i get the execfile to stop but the others to keep running?
The part of 1.py with exit() is:
try :
Ntot=10000
x,y,s=myMCMC2D(Ntot,0.78,0.63,1,1)
except :
exit('error')
try:
execfile('1.py')
except SystemExit:
print "1.py exited"
Exit is an exception which can be caught.

How to stay in the cmd window when the exception has been raised?

I have written a programme by python which is successfully tested under eclipse.Then I used pyinstaller to excute it as a .exe file. When the programme raise the exception ,the cmd window will quit immediately. I want to stay in this window to take a good look at this exception. How can I do it ? Thank you.
As Ms Turdy mentioned, you should run it in a command prompt or terminal first, if it will have the same behavior as the exe.
You can execute a python script with python -m pdb script.py and it will enter into the debugger. You run it by pressing C for continue, then it will break when it raises the exception.
That is because the python script has finished its job. You can do this:
import time
# your code
...
time.sleep(20)
This will give you 20 seconds to see the result. And after 20 s, the cmd window will remain 20 s for you to see the result. You can change the time for your requirement.
You can try raw_input to hold the screen:
import traceback
try:
# do something dangerous
except Exception, e:
print 'Error:', e
print traceback.format_exc()
raw_input('Input anything to end...')

Categories