How should I pass arguments when running Slicer in batch mode?

I want to run slicer from within python, to execute a script that builds a scene automatically from a JSON file telling it what files to load. I think I’ve solved everything except how to tell slicer the location of the JSON file. There are several options I can think of:

  • Run Slicer --python-script myscript.py, first setting an environment variable json_file that is then checked by myscript.py.
  • Run Slicer --python-code mymodule.myscript(<json_file>), where my code inserts the correct string for <json_file> before running slicer
  • Same as above, but run Slicer -c instead of Slicer --python_code

All of these feel pretty kludgy for something that seems like it would be common. Is there some other way I’m not thinking of? Is there a preferred way to give arguments when running a python script within Slicer as part of a batch job?

Everything after --python-script until the next recognizable flag is passed into the python interpreter:

Run:
./Slicer.exe --no-main-window --python-script test.py param1 param2 param3

Where test.py is:
print(sys.argv)

You will get this output on the console:
['test.py', 'param1', 'param2', 'param3']

3 Likes