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

**URL:** <https://discourse.slicer.org/t/assertionerror-in-distutils-while-installing-torch-with-pytorchutils-for-segment-anything-model-based-extension/41167>\
**Category:** Support\
**Created:** [January 20, 2025, 4:02pm UTC](https://discourse.slicer.org/t/assertionerror-in-distutils-while-installing-torch-with-pytorchutils-for-segment-anything-model-based-extension/41167 "2025-01-20T16:02:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oothomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oothomas/32/68743_2.png) [@oothomas](https://discourse.slicer.org/u/oothomas)\
**Post date:** [January 20, 2025, 4:02pm UTC](https://discourse.slicer.org/t/assertionerror-in-distutils-while-installing-torch-with-pytorchutils-for-segment-anything-model-based-extension/41167/1 "2025-01-20T16:02:45Z")

</div>

I’m developing an extension called **SlicerPhotogrammetry** that uses Meta’s [Segment Anything](https://github.com/facebookresearch/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

```auto
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

```auto
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

```auto
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](https://github.com/SlicerMorph/SlicerPhotogrammetry).
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!

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/3/037065d7756af75398fbfdc6068856857496631e.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/b/fbfe4469569b291f0304d5def5f98b5a69f7b728.png)

---

<div class="post-metadata">

**Author:** ![oothomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oothomas/32/68743_2.png) [@oothomas](https://discourse.slicer.org/u/oothomas)\
**Post date:** [January 20, 2025, 4:38pm UTC](https://discourse.slicer.org/t/assertionerror-in-distutils-while-installing-torch-with-pytorchutils-for-segment-anything-model-based-extension/41167/2 "2025-01-20T16:38:22Z")

</div>

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.
