# Load python script from harddisk in Slicers´s Python Interactor?

**URL:** https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399
**Category:** Support
**Created:** [April 30, 2021, 7:16pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399 "2021-04-30T19:16:47Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)
#### Post date: [April 30, 2021, 7:16pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/1 "2021-04-30T19:16:47Z")

</div>

I was looking for that for ages about a month ago, so I just want to share the solution here (it may be obvious to you experts):

You can load and run a python script from the hard drive in the Python interactor (one-line command) with

```auto
    >> exec(open("filename.py").read())

```

This works in Slicer 4.11+ and seems to require Python3.

In Windows I used:

```auto
    >> exec(open(r"C:\Users\Yourname\Python\test.py").read())

```

Use slashes and do not forget the initial “r” …

Regards  
Rudolf

---

<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: [April 30, 2021, 7:23pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/2 "2021-04-30T19:23:06Z")

</div>

Yes, that’s a very handy pattern. I use it a lot for quick testing. It has the advantage that any global variables define in the .py file are still around in the python terminal for further testing. I typically put that command with the path as a comment at the top of my script file so I can easily copy-paste it into the terminal when I start a new slicer.

---

<div class="post-metadata">

### Author: ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)
#### Post date: [April 30, 2021, 7:24pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/3 "2021-04-30T19:24:39Z")

</div>

Haha that is exactly what I did, but forgot about my comment and searched the whole internet again today 🙂

---

<div class="post-metadata">

### Author: ![dalv.silvermann](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dalv.silvermann/32/16193_2.png) [@dalv.silvermann](https://discourse.slicer.org/u/dalv.silvermann)
#### Post date: [October 13, 2022, 8:37pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/4 "2022-10-13T20:37:13Z")

</div>

Dear Rudolf Bumm,  
Amazing! I’ve try to find this solution more than year I think :0)  
PS Also thanks for your MONAI label video presentation in summer '22!

---

<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: [October 25, 2022, 5:01am UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/5 "2022-10-25T05:01:10Z")

</div>

FYI, I’ve made some improvements in the past few days (will be available in the Slicer Preview Release from Wednesday):

- Instead of `exec(open(path).read())` you can hit Ctrl-r. Last path is remembered, so you can rerun a script using Ctrl-r, Enter.
- If you copy-paste code into an empty line then instead of line-by-line execution, the entire code on the clipboard is executed at once (fixing the annoying indentation errors due to presence of empty lines).

---

<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: [October 25, 2022, 12:41pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/6 "2022-10-25T12:41:39Z")

</div>

I wish we could try to follow ipython hotkey behavior wherever possible. `Ctrl-r` is incremental backward search in ipython and other readline based prompt systems and it’s a commonly used shortcut. Many of the common keyboard shortcuts]([Keyboard Shortcuts in the IPython Shell | Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/01.02-shell-keyboard-shortcuts.html)) like `Ctrl-e` , `Ctrl-y` etc work already in our python interactor and if we are adding features it’s nice to follow existing patterns.

But I should also add that I like the new features - they will save a lot of time!

---

<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: [October 25, 2022, 12:48pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/7 "2022-10-25T12:48:40Z")

</div>

It should not be a problem to switch the Ctrl-R shortcut to a more standard one now, especially because previously this feature have been practically unusable. I can push an update today. What should be the keyboard shortcut?

---

<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: [October 25, 2022, 12:51pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/8 "2022-10-25T12:51:09Z")

</div>

Thanks!

I’m not sure what would be best. Let’s discuss at the developer meeting. There are some existing cross platform issues we could address at the same time and come up with a good scheme so that slicer hotkeys are as compatible as possible with other hotkey conventions.

---

<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: [October 25, 2022, 1:19pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/9 "2022-10-25T13:19:45Z")

</div>

I’ll also add Ctrl-l for clearing the terminal.

Ctrl-r for reverse search could be useful and it would not be hard to add, especially if we do it in a popup window instead of struggling with using the terminal window for it (IPython shell uses a special mode with the `(reverse-i-search)` prompt, while we could simply open a small search box).

We could also easily add Ctrl-k/u to cut content before/after the cursor, but it seems marginally useful (you can do Shift+Home/End, Ctrl-x instead).

However, most other shortcuts don’t seem to make sense for me, such as using

- Ctrl-a/e instead of Home/End keys
- Ctrl-b/f instead of left/right arrow keys
- Ctrl-p/n instead of up/down arrow keys
- Ctrl-d instead of Delete key
- Ctrl-t for switching the previous two characters (really, a shortcut for this??)
- Ctrl-y instead of Ctrl-v for pasting

---

<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: [October 25, 2022, 3:37pm UTC](https://discourse.slicer.org/t/load-python-script-from-harddisk-in-slicers-s-python-interactor/17399/10 "2022-10-25T15:37:29Z")

</div>

Ctrl-r is the one I really care about. The rest are very common for people used to emacs-based features.  
For me, even the chrome text window I’m using on mac allows Ctrl-[bfnpeatkyt] and that’s handy for people like me who are used to them from bash or ipython. Yes, even Ctrl-t to transpose two characters works, although I don’t use that one.

After discussion on the slicer dev call, we think Ctrl-g is not otherwise used and can be associated with the concept of ‘go’.
