# "Download and install extension" snippet thows exception

**URL:** <https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849>\
**Category:** Development\
**Created:** [September 25, 2021, 10:26am UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849 "2021-09-25T10:26:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 25, 2021, 10:26am UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/1 "2021-09-25T10:26:55Z")

</div>

Windows  
Slicer 4.13

The script repository snippet “[Download and install an extension](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#download-and-install-extension)”  
throws a “Key error: ‘item\_id’” exception when used in Slicer 4.13

Has anything changed ?  
Thank you.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [September 25, 2021, 4:33pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/3 "2021-09-25T16:33:04Z")

</div>

Thanks for the report 🙏

> [@rbumm](#):
>
> Has anything changed ?

Indeed, we updated the infrastructure and some the API details changed.

For reference, see [Downloading extensions for older releases](https://discourse.slicer.org/t/downloading-extensions-for-older-releases/19256)

Waiting we update the documentation and fix the legacy server to support this snippet, when using Slicer 4.13, you could do this:

```diff
extensionName = 'SlicerIGT'
em = slicer.app.extensionsManagerModel()
if not em.isExtensionInstalled(extensionName):
  extensionMetaData = em.retrieveExtensionMetadataByName(extensionName)
- url = em.serverUrl().toString()+'/download/item/'+extensionMetaData['item_id']
- extensionPackageFilename = slicer.app.temporaryPath+'/'+extensionMetaData['_id']
+ url = em.serverUrl().toString() + '/download?items='+extensionMetaData['_id']
+ extensionPackageFilename = slicer.app.temporaryPath+'/'+extensionMetaData['_id']
  slicer.util.downloadFile(url, extensionPackageFilename)
  em.installExtension(extensionPackageFilename)
  slicer.util.restart()

```

For convenience, here is the full snippet for easy copy-paste:

```python
extensionName = 'SlicerIGT'
em = slicer.app.extensionsManagerModel()
if not em.isExtensionInstalled(extensionName):
  extensionMetaData = em.retrieveExtensionMetadataByName(extensionName)
  url = em.serverUrl().toString() + '/download?items='+extensionMetaData['_id']
  extensionPackageFilename = slicer.app.temporaryPath+'/'+extensionMetaData['_id']
  slicer.util.downloadFile(url, extensionPackageFilename)
  em.installExtension(extensionPackageFilename)
  slicer.util.restart()

```

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [September 25, 2021, 4:39pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/4 "2021-09-25T16:39:36Z")

</div>

> Waiting we […] fix the legacy server

Issue is now tracked in

> <https://github.com/KitwareMedical/slicer-extensions-legacy-webapp/issues/2>
>
> When using Slicer 4.13, the following snippet is expected to work:
> 
> 
> \`\`\`pyth…on
> extensionName = 'SlicerIGT'
> em = slicer.app.extensionsManagerModel()
> if not em.isExtensionInstalled(extensionName):
> extensionMetaData = em.retrieveExtensionMetadataByName(extensionName)
> url = em.serverUrl().toString()+'/download/item/'+extensionMetaData\['item\_id'\]
> extensionPackageFilename = slicer.app.temporaryPath+'/'+extensionMetaData\['md5'\]
> slicer.util.downloadFile(url, extensionPackageFilename)
> em.installExtension(extensionPackageFilename)
> slicer.util.restart()
> \`\`\`
> 
> For reference, see https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849
> 
> Changes to implement:
> \* Add route \`/download/item/\<id\>\`
> \* In addition of \`\_id\`, add \`item\_id\` to the payload
> \* Fill \`md5\` field .... this should probably be done only if this legacy route is used as it is "compute intense", or we could simply compute the hash of \`\<package name\>\`

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 25, 2021, 6:44pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/5 "2021-09-25T18:44:19Z")

</div>

> Waiting we update the documentation and fix the legacy server to support this snippet, when using Slicer 4.13, you could do this:

Thank you @jcfr .

I tested the new snippet in 4.13.0-2021-09-11 r30174 / 0db2e6e but got:

urllib.error.HTTPError: HTTP Error 404: Not Found

Failed to download file from [https://slicer-packages.kitware.com/download?items=613dbbc4342a877cb3bf8bd4](https://slicer-packages.kitware.com/download?items=613dbbc4342a877cb3bf8bd4)

Any ideas ?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [September 25, 2021, 7:04pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/6 "2021-09-25T19:04:55Z")

</div>

I initially thought this was for the latest stable release, for the preview build, you could do that:

Replace `em.serverUrl().toString()` with `“[https://slicer.kitware.com/midas3/](https://slicer.kitware.com/midas3/)”’

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 25, 2021, 7:46pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/7 "2021-09-25T19:46:42Z")

</div>

> [@jcfr](#):
>
> “[https://slicer.kitware.com/midas3/”](https://slicer.kitware.com/midas3/%E2%80%9D)

This seems to work. Thank you

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 26, 2021, 7:14am UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/8 "2021-09-26T07:14:43Z")

</div>

Unfortunately, it is not quite working yet.

If I use this code in 4.13:

```auto
        extensionName = 'SlicerIGT'
        em = slicer.app.extensionsManagerModel()
        if not em.isExtensionInstalled(extensionName):
          extensionMetaData = em.retrieveExtensionMetadataByName(extensionName)
          url = "https://slicer.kitware.com/midas3" + '/download?items='+extensionMetaData['_id']
          extensionPackageFilename = slicer.app.temporaryPath+'/'+extensionMetaData['_id']
          slicer.util.downloadFile(url, extensionPackageFilename)
          em.installExtension(extensionPackageFilename)
          slicer.util.restart()

```

there is no error message or exception, but the extension that is installed seems not to be the right version:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/c/6c82b967599184b14f2f7b333d83d64855c17a89.png)

If I install SlicerIGT from the extension manager I seem to get the correct version:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/5/c507b64c0b529a14764ea839e6c5ed12ec917360.png)

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 30, 2021, 3:10pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/9 "2021-09-30T15:10:13Z")

</div>

Obviously, this URL  
[https://slicer.kitware.com/midas3/download?items=613dbbc4342a877cb3bf8bd4](https://slicer.kitware.com/midas3/download?items=613dbbc4342a877cb3bf8bd4)  
downloads SlicerIGT from the extension server which is no longer active in 4.13.  
That is why the extension is older and shows outdated in 4.13.

But what would be the download URL to be used from 4.13 and to get the latest extension version?

---

<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:** [September 30, 2021, 4:40pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/10 "2021-09-30T16:40:45Z")

</div>

To simplify things, here is a script that works without any changes in both Slicer-4.11 and Slicer-4.13:

```python
extensionName = 'SlicerIGT'
em = slicer.app.extensionsManagerModel()
if not em.isExtensionInstalled(extensionName):
    extensionMetaData = em.retrieveExtensionMetadataByName(extensionName)
    if slicer.app.majorVersion*100+slicer.app.minorVersion < 413:
        # Slicer-4.11
        itemId = extensionMetaData['item_id']
        url = f"{em.serverUrl().toString()}/download?items={itemId}"
    else:
        # Slicer-4.13
        itemId = extensionMetaData['_id']
        url = f"{em.serverUrl().toString()}/api/v1/item/{itemId}/download"
    extensionPackageFilename = slicer.app.temporaryPath+'/'+itemId
    slicer.util.downloadFile(url, extensionPackageFilename)
    em.installExtension(extensionPackageFilename)
    slicer.util.restart()

```

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 30, 2021, 5:37pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/11 "2021-09-30T17:37:57Z")

</div>

Thanks, but unfortunately this still loads an outdated version of the extension into 4.13:

```auto
Downloading from
  https://slicer-packages.kitware.com/api/v1/item/613dbbc4342a877cb3bf8bd4/download
as file
  C:/Users/Rudolf/AppData/Local/Temp/Slicer/613dbbc4342a877cb3bf8bd4
It may take a few minutes...

```

but result is:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/e/6eb63afb0f927ed3e51ba5e649187c8d8931d579.png)

---

<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:** [September 30, 2021, 5:43pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/12 "2021-09-30T17:43:22Z")

</div>

30174 was some random nightly build some time ago. Please use latest Slicer Stable Release or latest Slicer Preview Release and let us know if you have problems with any of those.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [September 30, 2021, 5:43pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/13 "2021-09-30T17:43:28Z")

</div>

@rbumm You’re likely not going to be able to stay on an older version of Slicer 4.13 from the early period of the extension manager transition where some fixes were still being worked out on the Slicer side. Including [Unable to manually install extension with ExtensionsManager · Issue #5840 · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/issues/5840).

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 30, 2021, 6:05pm UTC](https://discourse.slicer.org/t/download-and-install-extension-snippet-thows-exception/19849/14 "2021-09-30T18:05:58Z")

</div>

Thank you.  
The script now works with r30272 (preview) as well as the latest stable release.
