segLg = slicer.modules.segmentations.logic() AttributeError: module 'modules' has no attribute 'segmentations'

Are the modules not fully imported? What’s wrong in py?

segLg = slicer.modules.segmentations.logic()
AttributeError: module ‘modules’ has no attribute ‘segmentations’
print(dir(slicer.modules))
[‘AddManyMarkupsFiducialTestInstance’, ‘AtlasTestsInstance’, ‘BRAINSFitRigidRegistrationCrashIssue4139Instance’, ‘ColorLegendSelfTestInstance’, ‘CompareVolumesInstance’, ‘CropVolumeSelfTestInstance’, ‘CropVolumeSequenceInstance’, ‘DICOMEnhancedUSVolumePluginInstance’, ‘DICOMGeAbusPluginInstance’, ‘DICOMImageSequencePluginInstance’, ‘DICOMInstance’, ‘DICOMPatcherInstance’, ‘DICOMScalarVolumePluginInstance’, ‘DICOMSlicerDataBundlePluginInstance’, ‘DICOMVolumeSequencePluginInstance’, ‘DataProbeInstance’, ‘EndoscopyInstance’, ‘ExtensionWizardInstance’, ‘FiducialLayoutSwitchBug1914Instance’, ‘ImportItkSnapLabelInstance’, ‘JRC2013VisInstance’, ‘JupyterNotebooksInstance’, ‘LandmarkRegistrationInstance’, ‘MarkupsInCompareViewersSelfTestInstance’, ‘MarkupsInViewsSelfTestInstance’, ‘MultiVolumeImporterInstance’, ‘MultiVolumeImporterPluginInstance’, ‘NeurosurgicalPlanningTutorialMarkupsSelfTestInstance’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘dicomPlugins’, ‘registrationPlugins’]

However in python console this situation does not occur.

image

Is it a build you did yourself or downloaded and installed?

Are there any errors on startup?

downloaded and installed from https://download.slicer.org/bitstream/67c52e9629825655577d0353

That is Slicer 5.8.1 for mac, and when I run it this works:

>>> slicer.modules.segmentations.logic()
<vtkSlicerSegmentationsModuleLogicPython.vtkSlicerSegmentationsModuleLogic(0x600003b38af0) at 0x1b4b7f100>

Please answer this question too, this is the more important one. Thanks

You’ll need to narrow down to something reproducible or we can’t help you out.

# ...docs/segLg.py
import slicer
segLg = slicer.modules.segmentations.logic()

For example, add the above file docs/segLg.py to modules as follows:

**The error is as follows:**

Ah, okay. Your module is being discovered before the segmentations has loaded.

You need something like:

self.parent.dependencies = ["Segmentations"]

in your module’s main class.

2 Likes

@pieper For example, how to modify segLg.py :backhand_index_pointing_down:?

import slicer
# ? self.parent.dependencies = ["Segmentations"]
segLg = slicer.modules.segmentations.logic()

Since you added this docs path to the module path it needs to be in the scripted module format, like you get when you use the ExtensionWizard. That’s there the dependency is expressed.

@pieper Teacher, could you be more specific? Could you give an example🌰?

Like here:

2 Likes
import slicer
# self.parent.dependencies = ["Segmentations"]
print(f'{dir(slicer.modules)=}')
segLg = slicer.modules.segmentations.logic()
dir(slicer.modules)=['AddManyMarkupsFiducialTestInstance', 'AtlasTestsInstance', 'BRAINSFitRigidRegistrationCrashIssue4139Instance', 'ColorLegendSelfTestInstance', 'CompareVolumesInstance', 'CropVolumeSelfTestInstance', 'CropVolumeSequenceInstance', 'DICOMEnhancedUSVolumePluginInstance', 'DICOMGeAbusPluginInstance', 'DICOMImageSequencePluginInstance', 'DICOMInstance', 'DICOMPatcherInstance', 'DICOMScalarVolumePluginInstance', 'DICOMSlicerDataBundlePluginInstance', 'DICOMVolumeSequencePluginInstance', 'DataProbeInstance', 'EndoscopyInstance', 'ExtensionWizardInstance', 'FiducialLayoutSwitchBug1914Instance', 'ImportItkSnapLabelInstance', 'JRC2013VisInstance', 'JupyterNotebooksInstance', 'LandmarkRegistrationInstance', 'MarkupsInCompareViewersSelfTestInstance', 'MarkupsInViewsSelfTestInstance', 'MultiVolumeImporterInstance', 'MultiVolumeImporterPluginInstance', 'NeurosurgicalPlanningTutorialMarkupsSelfTestInstance', 'PedicleScrewPlannerInstance', 'PedicleScrewSimulatorInstance', 'PedicleTrianglePlannerInstance', 'PerformanceTestsInstance', 'PlotsSelfTestInstance', 'PluggableMarkupsSelfTestInstance', 'PyTorchUtilsInstance', 'RSNA2012ProstateDemoInstance', 'RSNAQuantTutorialInstance', 'RSNAVisTutorialInstance', 'SampleDataInstance', 'ScenePerformanceInstance', 'ScreenCaptureInstance', 'SegmentEditorInstance', 'SegmentStatisticsInstance', 'SelfTestsInstance', 'SequencesSelfTestInstance', 'ShaderPropertiesInstance', 'SimpleFiltersInstance', 'SliceLinkLogicInstance', 'Slicer4MinuteInstance', 'SlicerBoundsTestInstance', 'SlicerDisplayNodeSequenceTestInstance', 'SlicerMRBMultipleSaveRestoreLoopTestInstance', 'SlicerMRBMultipleSaveRestoreTestInstance', 'SlicerMRBSaveRestoreCheckPathsTestInstance', 'SlicerNNUNetInstance', 'SlicerOrientationSelectorTestInstance', 'SlicerPythonTestRunnerInstance', 'SlicerScriptedFileReaderWriterTestInstance', 'SubjectHierarchyCorePluginsSelfTestInstance', 'SubjectHierarchyGenericSelfTestInstance', 'SurfaceToolboxInstance', 'TablesSelfTestInstance', 'TotalSegmentatorInstance', 'UtilTestInstance', 'VectorToScalarVolumeInstance', 'ViewControllersSliceInterpolationBug1926Instance', 'VolumeRenderingSceneCloseInstance', 'WebEngineInstance', 'WebServerInstance', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'dicomPlugins', 'registrationPlugins', 'sampleDataSources', 'sceneImport2428Instance']
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.9/imp.py", line 169, in load_source
    module = _exec(spec, sys.modules[name])
  File "<frozen importlib._bootstrap>", line 613, in _exec
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/liguimei/Documents/ppPrj/Resources/docs/segLg.py", line 4, in <module>
    segLg = slicer.modules.segmentations.logic()
AttributeError: module 'modules' has no attribute 'segmentations'
[Qt] loadSourceAsModule - Failed to load file "/Users/liguimei/Documents/ppPrj/Resources/docs/segLg.py"  as module "segLg" !
[Qt] Fail to instantiate module  "segLg"

Still don’t understand…

@lassoan ?…………………………………………….