Running python script in slicer in linux

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.

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\’

inside, it is just print(1+2)

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

these all failed

start Slicer home\user\project\mice\
Slicer.exe --python-script "'home\user\project\mice\" 

Syntaxx.Error: invalid syntax


There are multiple typos:

  1. user either apostrophe or double quotes.

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


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'

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

(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\\
Slicer.exe --python-script “home\\user\\project\\mice\\”

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

In the Slicer Python console you can

# run Python script