Calling slicer module from a python file

i know we can call the slicer module with command line arguments or calling from a batch file, can i write a python file to call slicer with same same command?

Yes, of course, you can use standard Python functions to run a process. See for example how it is done in slicer.util.launchConsoleProcess method:

i tried to run slicer using subprocess.Popen and Subprocess.run , but the thing is it is not recognizing the code after the slicer path like : slicer.exe --python-code “slicer.module…” , it just runs the slicer and now further action.
i’ll give a try to the example you’ve sent and let you know, i think i’m missing something :frowning:

It is all just standard Python, so you can find thousands of examples and many tutorials, documentation on the web. If you still have trouble then copy your complete script here so that we can have a look.

yeah, and i found it :slight_smile:

thank you so much

1 Like

Was there anything that you learned that could help others who will have similar needs in the future? Can you share the code snippet that ended up working?

Yes here is my code snippet

import os
import subprocess

os.chmod(‘C:\Program Files\Slicer 4.10.2\slicer.exe’,0o777)

args1= “‘C:\dicomfiles\study’”

arsg2= “‘any other args if any’”

args3 = 'C:\Program Files\Slicer 4.10.2\slicer.exe --python-code ’
args = args3 + args1 + args2
proc = subprocess.Popen(args)
proc.communicate()
returnCode = proc.poll()

the first thing is i dont know why without the os.chmod i got permission issue,
also any easy way to pass the path as args, as if folder name start with A or T it didn’t work :frowning:

This community is awesome :smiley:

1 Like

Backslash \ is an escape character in Python string literals. See in this post how you can use backslash characters:

1 Like

A post was split to a new topic: Is Slicer’s Python environment complete?