Running python file in Slicer on Mac

Can anyone help me with the commands here? I want to run a deep learning model in the 3D slicer. I have the file on my desktop. I tried to run it on a mac terminal as well as on 3D-Slicer. I am not able to proceed further. It is showing invalid syntax.

  • Anyone please help me here with the commands.

Thank you

Deep learning works well on Windows and Linux and should work on some macOS systems, too. Please provide more information about your system (what CPU, GPU, and operating system do you use), what commands you execute and errors you get.

-Apple M1 chip
-8-core CPU
-8-core GPU

I am completely new to this software. Any references or any links related to this is very helpful for me.

The command I used was from one of the discussions in this forum

/Applications/Slicer.app/Contents/MacOS/Slicer --no-splash --no-main-window --python-script /Users/ashu/Desktop/test.py

This looks good overall. What is in test.py? What error message do you get?

The file has a complete python code with nothing related to the slicer. My code does produce results when running on Google Colab. I want it to show output in 3d Slicer

qSlicerMarkupsModulePrivate::addToolBar: no main window is available, toolbar is not added
qSlicerSequencesModulePrivate::addToolBar: no main window is available, toolbar is not added
Traceback (most recent call last):
File “”, line 5, in < module >
File “”, line 421, in < module >
NameError: name ‘null’ is not defined

Hi all,
Any solution for this issue? I am facing the same issue on my ubuntu environment.
Thanks!

To better help, it wold be good if you could post a small example and describe how you are trying to run it.

Hi Steve,

This is what I run in the command line:

‘/home/dellxpsazdelta/Slicer-5.2.2-linux-amd64/Slicer’ --no-main-window --python-script /home/dellxpsazdelta/Documents/Lukas/automatisation/segmentationVolumes.py

and this is the output:

Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
qSlicerMarkupsModulePrivate::addToolBar: no main window is available, toolbar is not added
qSlicerSequencesModulePrivate::addToolBar: no main window is available, toolbar is not added
Traceback (most recent call last):
File “”, line 5, in
File “”, line 6, in
File “/home/dellxpsazdelta/Slicer-5.2.2-linux-amd64/bin/Python/slicer/util.py”, line 1145, in moduleSelector
raise RuntimeError(“Could not find main window”)
RuntimeError: Could not find main window

To summarize: I am trying to run the segmentationVolumes.py python script in Slicer without using the GUI. When I execute the script in the 3D Slicer application, I runs without any error.

Thanks for the help.

Ah, okay, you should upgrade to Slicer 5.4.0. There’s now just a warning.

I updated slicer to 5.4.0 but I still have the same issue:

Input: ‘/home/dellxpsazdelta/Slicer-5.4.0-linux-amd64/Slicer’ --no-main-window --python-script /home/dellxpsazdelta/Documents/Lukas/automatisation/segmentationVolumes.py

Output: Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
qSlicerMarkupsModulePrivate::addToolBar: no main window is available, toolbar is not added
qSlicerSequencesModulePrivate::addToolBar: no main window is available, toolbar is not added
Traceback (most recent call last):
File “”, line 5, in
File “”, line 6, in
File “/home/dellxpsazdelta/Slicer-5.4.0-linux-amd64/bin/Python/slicer/util.py”, line 1163, in moduleSelector
raise RuntimeError(“Could not find main window”)
RuntimeError: Could not find main window

Please not that in the beginning of the output, I indeed receive a warning, but however at the end, I do receive a runtimeError.

Any other solution as updating Slicer did not resolve the issue? Thanks!

Please post the contents of the segmentationVolumes.py script, or better, just enough of it to reproduce the RuntimeError.

I see, you are calling selectModule but you pass the --no-main-window flag which doesn’t work because modules require a user interface in order to be selected (selecting builds their Qt interface).

Since you are using the MONAILabel extension you’ll need to look into the code to see how to use it with no GUI. Slicer modules should always expose a Logic class that implements the non-GUI aspects of the code for exactly this use case, but sometimes modules aren’t written that way, at least at first, because developers don’t have this use case in mind. You can read through the MONAILabel code to see how to accomplish what you are doing using just the logic class. I haven’t looked, but this might require some changes to the module or duplicating some code in your script if you just want a workaround.