Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JupyterLab: Stop the execution of a code at a line

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?

like image 780
ASE Avatar asked Oct 28 '25 04:10

ASE


1 Answers

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.

like image 68
mujjiga Avatar answered Oct 30 '25 15:10

mujjiga