# Calling module with slicer functions fail in Jupyter notebook

**URL:** <https://discourse.slicer.org/t/calling-module-with-slicer-functions-fail-in-jupyter-notebook/17712>\
**Category:** Support\
**Tags:** python, jupyter\
**Created:** [May 20, 2021, 3:50pm UTC](https://discourse.slicer.org/t/calling-module-with-slicer-functions-fail-in-jupyter-notebook/17712 "2021-05-20T15:50:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mc\_barbour](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mc\_barbour](https://discourse.slicer.org/u/mc_barbour)\
**Post date:** [May 20, 2021, 3:50pm UTC](https://discourse.slicer.org/t/calling-module-with-slicer-functions-fail-in-jupyter-notebook/17712/1 "2021-05-20T15:50:08Z")

</div>

I’m currently using slicer via jupyter. I’ve written a number of functions to help me batch segment 4dct data and I’m attempting to load those functions into my notebook via a module as I’ve got multiple patients to evaluate. When I try to access the functions, however, I get "name ‘slicer’ is not defined.

When the function is defined from within my notebook, it executes without an issue. I’ve checked the python path from within the module functions and it points to all the slicer-python locations.

Is there something special I need to do to import this module which calls numerous slicer executables?

OS: MacOS, 10:15  
slicer: 4.11.20201026

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 20, 2021, 5:07pm UTC](https://discourse.slicer.org/t/calling-module-with-slicer-functions-fail-in-jupyter-notebook/17712/2 "2021-05-20T17:07:45Z")

</div>

The `slicer` namespace is only available if you use the Slicer kernel. You can choose the kernel when you create the notebook or later in the notebook’s menu (Kernel → Change kernel).

---

<div class="post-metadata">

**Author:** ![mc\_barbour](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mc\_barbour](https://discourse.slicer.org/u/mc_barbour)\
**Post date:** [May 20, 2021, 7:18pm UTC](https://discourse.slicer.org/t/calling-module-with-slicer-functions-fail-in-jupyter-notebook/17712/3 "2021-05-20T19:18:44Z")

</div>

Right, I do start the notebook with the slicer kernel and the slicer gui launches. I then try to import a module in which I’ve written some functions in that also use the slicer namespace.

The notebook loads the module okay. Since the functions are not explicitly defined within the notebook, but loaded, does it not have access to the slicer namespace? I’ve included a section of the module that I’m trying to load into the notebook which is running the slicer kernel.

```python
import sys
import slicer

def setup_airway_segment_editor(segmentationNode=None, masterVolumeNode=None):
    
"""
Runs standard setup of segment editor widget and segment editor node
"""
    if segmentationNode is None:
    # Create segmentation node
        segmentationNode = slicer.vtkMRMLSegmentationNode()
        slicer.mrmlScene.AddNode(segmentationNode)
        segmentationNode.CreateDefaultDisplayNodes()
    
    segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
    slicer.mrmlScene.AddNode(segmentEditorNode)
    segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget.setSegmentationNode(segmentationNode)
    segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
    if masterVolumeNode:
        segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
    
    # Add airway segment
    airwaySegmentID = segmentationNode.GetSegmentation().AddEmptySegment("Airway")
    
    # Add mask segment
    maskLabelMap = slicer.util.getNode('maskLabelMap')
    slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(maskLabelMap, segmentationNode)
    maskSegmentID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('mask')

    return segmentEditorWidget, segmentEditorNode, segmentationNode, airwaySegmentID, maskSegmentID

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 20, 2021, 7:32pm UTC](https://discourse.slicer.org/t/calling-module-with-slicer-functions-fail-in-jupyter-notebook/17712/4 "2021-05-20T19:32:15Z")

</div>

You can add functions to the `global()` namespace or `slicer` namespace (by simply using the `=` operator). However, normally you would not want to pollute these global namespaces but instead add helper functions to your module’s namespace or into your module logic, which you can import and use from anywhere.

For example, you can reach `SampleData` module functions logic like this:

1. Run function specified in a module’s namespace:

```python
from SampleData import *

downloadSample('MRHead')

```

1. Run method specified in a module logic class:

```python
import SampleData

logic = SampleData.SampleDataLogic()
logic.downloadCTACardio()

```
