3

I have a long code that sometimes I do not want to execute all of the code, but just stop at a certain line. To stop the execution of a code at a line, I do the following:

print('Stop here: Print this line')
quit()
print('This line should not print because the code should have stopped')

The right answer is only the first line should print. When I use quit(), quit , exit(), or exit both lines print. When I use import sys and then sys.exit() I get the following error message

An exception has occurred, use %tb to see the full traceback. SystemExit C:\Users\user\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3351: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

How can I perform this task of stopping execution at a line?

ASE
  • 1,702
  • 2
  • 21
  • 29
  • Am I right assuming you are doing this for the sake of debugging? – MaKaNu Nov 30 '20 at 14:53
  • Have you tried raising an exception? You can add something like ```raise ValueError``` which will throw an exception. – Matt C Nov 30 '20 at 14:53
  • @MaMaNu: Yes for debugging mostly, but also sometimes I just want to execute the code up to a point. – ASE Nov 30 '20 at 14:55
  • @Matt C: How to do this? When I added ````raise ValueError```` I got an error --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in – ASE Nov 30 '20 at 14:57
  • `sys.exit(0)` is the right way to exit normally from a program. Exit code `0` tells the system, that there was no error. If IPython sees this as a problem and posts a warning, then it's a IPython "problem" – user8408080 Nov 30 '20 at 15:00
  • I tested it on an none Interactive Environment and the ```sys.exit()``` or the ```quit()``` are working without any Message. I think the warning you get in the Interactive Jupyter Env just results of a unusaul behaviour, just like the Exception which you get if using ```ctrl + C``` to stop a running programm from CLI – MaKaNu Nov 30 '20 at 15:01

5 Answers5

3

In case you are trying to debug the code and may want to resume from where it stopped, you should be using pdb instead

print('Stop here: Print this line')
import pdb; pdb.set_trace()
print('This line should not print because the code should have stopped')

When you execute it, the interpreter will break at that set_trace() line. You will then be prompted with pdb prompt. You can check the values of variable at this prompt. To continue execution press c or q to quit the execution further. Check other useful command of pdb.

mujjiga
  • 16,186
  • 2
  • 33
  • 51
1

It appears that you would like to stop the code at a certain point of your choosing

To do this I found two possible ways within your constraints. One is that you simply write

raise Exception("Finished code")

This would allow you to stop the code and raise your own exception and write whatever exception you so choose.

However, if you would like to not have any exception whatsoever then I would point you to this link: https://stackoverflow.com/a/56953105/14727419.

Avaye Dawadi
  • 76
  • 1
  • 2
  • When I run this code, I get this error "--------------------------------------------------------------------------- Exception Traceback (most recent call last) in 1 print('Stop here: Print this line') ----> 2 raise Exception("Finished code") 3 print('This line should not print because the code should have stopped') Exception: Finished code – ASE Nov 30 '20 at 16:35
  • Yes so that is you raising the exception "Finished code." It is not a mistake because that is what the raise command does in Python. At the link I posted, you should see a way to raise that exception without having the exception message given back to you. – Avaye Dawadi Nov 30 '20 at 18:06
0

It seems to be an issue related to iPython, as seen here.

If you don't wish to use the solution provided there, and don't mind forcefully killing the process, you can do:

import os

os.system('taskkill /F /PID %d' % os.getpid())
Shar
  • 448
  • 3
  • 8
0

For your debugging purposes it fine to use the builtin debugger pdb. The following link gives a tutorial how to set it up: Debug Jupyter

MaKaNu
  • 762
  • 8
  • 25
0

This is what I have to prevent unintentional execution of the Tests section at the bottom of the pipeline:

import pdb

# this line should capture the input, temporarily 
# preventing subsequent notebook cells from being executed
pdb.set_trace() 

# this line causes pdb to exit with an error, which is required 
# to stop subsequent cells from execution if user fails to type 
# "exit" in pdb command line and just presses the Stop button in the Notebook interface
raise Error("This line should fail to prevent cells below from being executed regardless of how pdb was exited!")
mirekphd
  • 4,799
  • 3
  • 38
  • 59