# Stop python script without causing 3D Slicer to close

**URL:** <https://discourse.slicer.org/t/stop-python-script-without-causing-3d-slicer-to-close/34463>\
**Category:** Development\
**Tags:** python\
**Created:** [February 22, 2024, 3:21am UTC](https://discourse.slicer.org/t/stop-python-script-without-causing-3d-slicer-to-close/34463 "2024-02-22T03:21:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teresa\_Marotta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/teresa_marotta/32/69440_2.png) [@Teresa\_Marotta](https://discourse.slicer.org/u/Teresa_Marotta)\
**Post date:** [February 22, 2024, 3:21am UTC](https://discourse.slicer.org/t/stop-python-script-without-causing-3d-slicer-to-close/34463/1 "2024-02-22T03:21:07Z")

</div>

Is there a way to stop a python script from running in the Python Console without closing 3D Slicer? I’m running a script to populate data into a table and I want to stop the script and export the data to a csv file. I tried using sys.exit() but this causes the application to close. Any help with this is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 22, 2024, 3:27am UTC](https://discourse.slicer.org/t/stop-python-script-without-causing-3d-slicer-to-close/34463/2 "2024-02-22T03:27:40Z")

</div>

`sys.exit()` terminates the current process, so if you run the script in the Slicer process then it will quit Slicer. If you don’t want to quit Slicer then you can put your code in a function and call `return` to quit from it at any point. You can also allow the user to cancel the processing by hitting a button; for this you just need to call `slicer.app.processEvents()` time to time in your processing code to allow the application to process GUI events.
