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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With