# Running python script in slicer in linux

**URL:** <https://discourse.slicer.org/t/running-python-script-in-slicer-in-linux/29124>\
**Category:** Support\
**Tags:** python\
**Created:** [April 26, 2023, 12:59am UTC](https://discourse.slicer.org/t/running-python-script-in-slicer-in-linux/29124 "2023-04-26T00:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![exngan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/exngan/32/65770_2.png) [@exngan](https://discourse.slicer.org/u/exngan)\
**Post date:** [April 26, 2023, 12:59am UTC](https://discourse.slicer.org/t/running-python-script-in-slicer-in-linux/29124/1 "2023-04-26T00:59:44Z")

</div>

Hi I tried to run python script or code in slicer in linux and windows (linux mainly). I followed this guideline, but none of the methods worked. I received syntax error everytime.  
[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html)

I used ‘/’ in linux and ‘\’ in windows for file path.  
I installed slicer in a folder ‘home\user\Software\Slicer-5.2.2-linux-amd64’  
The python script is installed in another folder like ‘home\user\project\mice\test.py’

inside test.py, it is just `print(1+2)`

```auto
import os
homepath = ''home\user\Software\Slicer-5.2.2-linux-amd64"
os,chdir(homepath) # this one worked

```

these all failed

```auto
start Slicer home\user\project\mice\test.py

```

```auto
Slicer.exe --python-script "'home\user\project\mice\test.py" 

```

Syntaxx.Error: invalid syntax

Thanks

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [April 26, 2023, 2:10am UTC](https://discourse.slicer.org/t/running-python-script-in-slicer-in-linux/29124/2 "2023-04-26T02:10:31Z")

</div>

> [@exngan](#):
>
> `homepath = ''home\user\Software\Slicer-5.2.2-linux-amd64"`

There are multiple typos:

1. user either apostrophe or double quotes.

Try  
`homepath = 'home\user\Software\Slicer-5.2.2-linux-amd64'`

or

`homepath = "home\user\Software\Slicer-5.2.2-linux-amd64"`

1. Linux paths start with / not backslash (`\`) like:

`homepath = '/home/user/Software/Slicer-5.2.2-linux-amd64'`

---

<div class="post-metadata">

**Author:** ![exngan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/exngan/32/65770_2.png) [@exngan](https://discourse.slicer.org/u/exngan)\
**Post date:** [April 26, 2023, 5:25am UTC](https://discourse.slicer.org/t/running-python-script-in-slicer-in-linux/29124/3 "2023-04-26T05:25:41Z")

</div>

some typos in the post, i dont know how to edit it. let me rephrase my question

I tested all the following codes in linux and windows. If in linux, I changed the separator to forward slash (/). In windows, I tested the codes with double blackslash “\\” and single blackslash “\” respectively . (For some reasons, I have to enter two blackslashes in order to show a single blackslash in this forum.)

import os  
homepath = ‘home\user\Software\Slicer-5.2.2-linux-amd64’ # / if in linux  
os.chdir(homepath)

(above 3 lines worked, no issues with them)

below all failed, even if I changed the separator for different operating systems

start Slicer home\\user\\project\\mice\\test.py  
Slicer.exe --python-script “home\\user\\project\\mice\\test.py”

or  
start Slicer home/user/project/mice/test.py  
Slicer.exe --python-script “home/user/project/mice/test.py”

---

<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 26, 2023, 8:33am UTC](https://discourse.slicer.org/t/running-python-script-in-slicer-in-linux/29124/4 "2023-04-26T08:33:40Z")

</div>

In the Slicer Python console you can

```auto
# run Python script
exec(open(r"C:\Users\Yourname\Documents\MySlicerExtensions\Python\test.py").read())

```
