# Python console : how to do these special tasks?

**URL:** <https://discourse.slicer.org/t/python-console-how-to-do-these-special-tasks/21272>\
**Category:** Support\
**Created:** [December 30, 2021, 11:49am UTC](https://discourse.slicer.org/t/python-console-how-to-do-these-special-tasks/21272 "2021-12-30T11:49:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [December 30, 2021, 11:49am UTC](https://discourse.slicer.org/t/python-console-how-to-do-these-special-tasks/21272/1 "2021-12-30T11:49:26Z")

</div>

1. While testing things, we may get trapped with variables previously declared and assigned in the Python console. I’m looking for a way to completely clear/clean/reset the Python console to avoid the need of restarting Slicer.

slicer.app.pythonConsole().reset() does not work as expected, previous declared variables persist.

1. How can we load/run a Python script saved on disk ? Something like ‘source’ in bash.

Thanks for any advice.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [December 30, 2021, 8:28pm UTC](https://discourse.slicer.org/t/python-console-how-to-do-these-special-tasks/21272/2 "2021-12-30T20:28:50Z")

</div>

When I’m just experimenting, I’ll often make [a script like this](https://github.com/pieper/facenav/blob/master/facenav.py) that conditionally initializes some bulk data (e.g. downloading a sample volume) if the node is not found and leaves the intermediate values in the python console for interactive query and also to test invoking methods.

Then I use a command like this:

```auto
path="c:/pieper/facenav/facenav.py"
exec(open(path).read())

```

to load the script which works like `source` in bash. It can typically just be re-run dozens of times while refining the logic. Then sometimes if the logic is wrong you may need to re-start Slicer but it just means the initialization needs to run again.

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [December 31, 2021, 10:50am UTC](https://discourse.slicer.org/t/python-console-how-to-do-these-special-tasks/21272/3 "2021-12-31T10:50:38Z")

</div>

> [@pieper](#):
>
> Then I use a command like this:

This works very well, thank you.
