Slicer module not found when using GetSubjectHierarchyNode


I was trying to get all the children of a folder in the subject hierarchy node using the convenience method slicer.mrmlScene.GetSubjectHierarchyNode. It didn’t work and I got the error:

  • NameError: name ‘slicer’ is not defined

Inspecting Slicer’s repo (I think this is the place to look in, but I’m not sure: Slicer/ at 1807944b939b81145ec2a049ef088e0d59d5039f · Slicer/Slicer · GitHub) I found which is, perhaps, the problem. It seems like that function is lacking the “import slicer” statement.

I’m relatively new to slicer so I’m not sure if this is correct. If it is, is there something I can do to patch it up? If not, could you please show me how to use this function?

Thank you very much in advance for your answers! Cheers,


What Python environment have you tried to run your code in?

In Slicer’s Python interpreter slicer package is already imported, but maybe in your module’s scope you need to call import slicer first.

Thank you for your reply Andras!

I now realized I didn’t write the correct function that is not working for me, which is slicer.util.getSubjectHierarchyItemChildren()

I am using Slicer’s Python interpreter in Slicer 4.11.20210226 r29738 / 7a593c8

To reproduce the error, you just need to write that function in the interpreter, after which I get the error mentioned before.

I also tried importing slicer before running that command, but it keeps throwing the same error. If I copy the function in the GitHub link that I provided in the interpreter, everything works, so I am not sure why it is having trouble finding the slicer module.

Good catch, somehow the import slicer, vtk statement was missed in that function. I’ve added it now and the fix will be available in the Slicer Preview Release from tomorrow. You can fix this now on your computer by updating this file.

1 Like