AssertionError in distutils while installing Torch with PytorchUtils for Segment Anything Model-based extension

I’m developing an extension called SlicerPhotogrammetry that uses Meta’s Segment Anything model. Because Segment Anything requires PyTorch (and thus Torch + TorchVision), I’m programmatically installing PyTorch inside the Slicer environment if it isn’t already installed.

Here is the relevant installation snippet in my SlicerPhotogrammetry.py (using the PyTorchUtils extension):

python

Copy

try:
    import PyTorchUtils
except ModuleNotFoundError:
    slicer.util.messageBox("SlicerPhotogrammetry requires the PyTorch extension. Please install it from the "
                           "Extensions Manager.")

torchLogic = PyTorchUtils.PyTorchUtilsLogic()
if not torchLogic.torchInstalled():
    logging.debug('SlicerPhotogrammetry requires the PyTorch Python package. Installing... '
                  'this may take several minutes')
    torch = torchLogic.installTorch(askConfirmation=True, forceComputationBackend='cu118')
    if torch is None:
        slicer.util.messageBox('PyTorch extension needs to be installed manually to use this module.')

try:
    from PIL import Image, ExifTags
except ImportError:
    slicer.util.pip_install("Pillow")
    from PIL import Image, ExifTags

try:
    import cv2
    if not hasattr(cv2, 'xfeatures2d'):
        raise ImportError("opencv-contrib-python is not properly installed")
except ImportError:
    slicer.util.pip_install("opencv-python")
    slicer.util.pip_install("opencv-contrib-python")
    import cv2

try:
    import segment_anything
except ImportError:
    slicer.util.pip_install("git+https://github.com/facebookresearch/segment-anything.git")
    import segment_anything

However, after the PyTorch installation completes, the module fails to load with an AssertionError pointing to:

bash

Copy

AssertionError: /home/username/Slicer-5.7.0-2025-01-11-linux-amd64/lib/Python/lib/python3.9/distutils/core.py

Below is part of the console log:

vbnet

Copy

Collecting torch
  Using cached https://download.pytorch.org/whl/cu118/torch-2.5.1%2Bcu118-cp39-cp39-linux_x86_64.whl (838.4 MB)
...
AssertionError: /home/username/Slicer-5.7.0-2025-01-11-linux-amd64/lib/Python/lib/python3.9/distutils/core.py
[Qt] loadSourceAsModule - Failed to load file "...SlicerPhotogrammetry.py" as module "SlicerPhotogrammetry" !
[Qt] Fail to instantiate module  "SlicerPhotogrammetry"
[Python] The module factory manager reported an error.

Steps to Reproduce:

  1. Clone the SlicerPhotogrammetry repository.
  2. Use Slicer’s Extension Wizard to “Select Extension” and build/install this extension from the cloned local folder. An error will occur
  3. If you restart Slicer and attempt to load or use the SlicerPhotogrammetry module, an error should occur.
  4. Observe that PyTorch is downloaded and installed, but an AssertionError related to distutils occurs afterward, causing the module to fail to load.

Environment Details:

  • Operating System: Linux (Ubuntu 22.04)
  • Slicer Version: 5.7.0-2025-01-11 (Preview Release)
  • PyTorch installed via torchLogic.installTorch(askConfirmation=True, forceComputationBackend='cu118').

Question:

  • How can I resolve the AssertionError due to distutils being replaced by setuptools?
  • Is there a recommended or more robust approach to install Torch/TorchVision (and dependencies like segment_anything) within Slicer so that Segment Anything can run without environment conflicts?

Any guidance or best practices for seamlessly installing PyTorch inside a Slicer extension would be greatly appreciated!


RESOLVED! I was checking for dependencies and installing in the constructor of the widget class due to another error with cv2 import. That has been resolved and now the dedicated load_dependencies method works as expected.

1 Like