Run python script from command line not working

Hi all,

I want to do the same processing to several volumes so it would be very useful to use slicer code from
command line.
What I want is: open volume → process → export processed volume. I guess I can do it with the same code I’m using in the python interactor one by one.

I’ve been searching in other posts how to do it. I tried:

C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe --python-script "C:\Users\Belen\Desktop\pruebaSlicer.py" -no-splash --no-main-window | more

and --launch python-real instead --python-script but none of them are working.

C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe opens slicer but C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe --help | more is not showing anything.

I do not know what I am doing wrong.

Thanks

Hi -

This has to do with the way windows handles output. You don’t always see output in the command console the way you would on a linux or mac system (some windows expert can explain the details). But Slicer does run the script and if you need to get output from it you can write to a file.

For example if you run this script you should see the output printed in the python console for a second before the program disappears. (Assumes you had the python console open when you last exited slicer and you don’t use the --no-main-window option).

print(40+2)
qt.QTimer.singleShot(1000, exit)
1 Like

I usually do batch processing within the same Slicer session. I process one dataset, close the scene (slicer.mrmlScene.Clear()) and can proceed with the next one.

Btw I also think your script should work and it may be an issue with the output the way you do it.

1 Like

Hi,

I runned this script but nothing is happening. It seems like Slicer is not running the code.

(base) C:\Users\Belen>C:\Users\Belen\AppData\Local\NA-MIC"Slicer 4.11.20210226"\Slicer.exe --python-script C:\Users\Belen\Desktop\pruebaSlicer.py

And pruebaSlicer.py:

print(40+2)
qt.QTimer.singleShot(1000, exit)```

Do you still have the three backtick characters after the second line? (the ```). These would be a python syntax error that might not be reported. Make sure whatever is in your script file works when typed into the python console in Slicer.

1 Like

No, I do not have the ``` (it was a typo). When I run the command, slicer did not even open. But it opens if I do not write “–python-script …”

Oh, I already make it. I changed directory to the slicer.exe path. It’s weird… thank you so much. I will try the processing code.

The problem was that Slicer executable path was specified with an incorrect syntax (misplaced quotation mark). It seems that Windows can tolerate this, but the command-line argument parser in the Slicer launcher is more strict.

Solution: a path that contain spaces must be quoted by placing a quotation mark at the beginning and at the end.

For example, instead of this (incorrect):

Slicer.exe path must be specified like this:

"C:\Users\Belen\AppData\Local\NA-MIC\Slicer 4.11.20210226\Slicer.exe"
2 Likes

Thanks for the explanation.
I noticed that when I run the script (no --no-main-window) the code continues running even when I close the program (slicer). How can I stop the execution without having to do it by killing a process?

You can call exit() when your processing is finished.

I meant something similar to ctrl+C for the cmd.

Slicer launcher (Slicer.exe) is built as a GUI application in the official Slicer installation packages. GUI applications are not killed if you hit Ctrl-C.

There is a trick that you can use if you really must use Ctrl-C instead of the X button. We include a console-mode launcher PythonSlicer.exe, you can rename that and replace the original GUI-mode Slicer.exe launcher. The console-mode launcher will respond to Ctrl-C on the latest Slicer Preview Release (because there the SlicerApp-real.exe executable is also built as a console application). However killing the process like this will not result in a clean exit, because objects are not destructed so VTK will report memory leaks. Stable releases might be built with error leak reporting disabled, but that will not stop on Ctrl-C (because in that version SlicerApp-real.exe is not a console application yet). If you want a solution for a clean exit from the command prompt using Ctrl-C then you need to build Slicer yourself or wait for the new Slicer Stable Release (expected in a week or so).

What is your overall goal? How would you like to launch and stop Slicer?

Ok, many thanks for the explanation.

I am just curious. I have codes that operate on many models and sometimes it is very useful to be able to terminate the execution of the program abruptly, in the same way as with another console program.

Your can use the latest Slicer Preview Release with the console-mode launcher as described above; and ignore the listed memory leaks.

Alternatively, you can terminate the process tree using Processes Explorer (Sysinternals utility); or look up the the SlicerApp-real.exe process and terminate it.

1 Like