# Install python library with extension

**URL:** https://discourse.slicer.org/t/install-python-library-with-extension/10110
**Category:** Development
**Tags:** extensions-manager
**Created:** [February 4, 2020, 5:55pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110 "2020-02-04T17:55:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)
#### Post date: [February 4, 2020, 5:55pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/1 "2020-02-04T17:55:10Z")

</div>

Hi all,  
the extension I’m developing needs an external python library.  
I can install it by using pip\_install in the python shell but, of course I need to embed this into the installation process.  
How can I do this?  
Thanks a lot.

Paolo

---

<div class="post-metadata">

### Author: ![markasselin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/markasselin/32/5916_2.png) [@markasselin](https://discourse.slicer.org/u/markasselin)
#### Post date: [February 4, 2020, 6:41pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/2 "2020-02-04T18:41:55Z")

</div>

Hi Paolo,

You can use the following structure to do this right in the module. The first time the module is loaded the library will be automatically installed, and every successive time the import in the try block will succeed and the catch will not be executed.

```
try:
    import library_name
except:
    slicer.util.pip_install('library_name')
    import library_name

```

Of course you can use other forms of the Python import lines as well, like  
`from library_name import module_name`

---

<div class="post-metadata">

### Author: ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)
#### Post date: [February 4, 2020, 6:44pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/3 "2020-02-04T18:44:55Z")

</div>

Thanks @markasselin!  
Yes, this is a possible solution, but I was looking for a way to do this in the _real_ installation step (maybe you install the extension and the first time you run the extension you have no internet access).

Thanks again

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [February 4, 2020, 7:11pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/4 "2020-02-04T19:11:17Z")

</div>

Hi Paulo -

Since the user needs internet to download the extension you are probably safe with the approach Mark suggested. If you want to be one step safer, you could include that code in a method called by when the `startupCompleted` signal is triggered. If you set this up in the module class it will be triggered every time Slicer starts up with your extension installed. Since they need to restart after installing the extension it’s pretty likely to happen while they are on the internet.

Something like this:

> <https://github.com/Slicer/Slicer/blob/6302522681e6a0bdcdb9f112634f94f3eb4f0097/Modules/Scripted/SampleData/SampleData.py#L87-L109>

---

<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: [February 4, 2020, 7:40pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/5 "2020-02-04T19:40:06Z")

</div>

The installation steps that @markasselin and @pieper described are correct.

You cannot install required Python packages during extension installation time, because extensions are shared between all Slicer instances of a specific version, while extension packages are not shared (but installed separately for each Slicer instance).

---

<div class="post-metadata">

### Author: ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)
#### Post date: [February 4, 2020, 9:08pm UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/6 "2020-02-04T21:08:44Z")

</div>

Tagging for when I forget how to do this.

---

<div class="post-metadata">

### Author: ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)
#### Post date: [February 5, 2020, 12:05am UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/7 "2020-02-05T00:05:31Z")

</div>

Thanks a lot!  
I used the strategy proposed by @markasselin, it is much more confortable!

Thanks again!  
Paolo

---

<div class="post-metadata">

### Author: ![Davide\_Punzo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/davide_punzo/32/66104_2.png) [@Davide\_Punzo](https://discourse.slicer.org/u/Davide_Punzo)
#### Post date: [February 5, 2020, 8:59am UTC](https://discourse.slicer.org/t/install-python-library-with-extension/10110/8 "2020-02-05T08:59:59Z")

</div>

Ciao @PaoloZaffino, if your extension is in the extension manager, you can actually do it directly in the cmake (so they will be packed at compilation time on the kitware factory machines):

1. add python requirements in the superbuild:  
[https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild.cmake#L28](https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild.cmake#L28)  
[https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild/slicerastro-requirements.txt](https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild/slicerastro-requirements.txt)  
[https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild/External\_python-slicerastro-requirements.cmake](https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild/External_python-slicerastro-requirements.cmake)

2. ensure the packing directly in Slicer build:  
[https://github.com/Punzo/SlicerAstro/blob/master/CMakeLists.txt#L169-L176](https://github.com/Punzo/SlicerAstro/blob/master/CMakeLists.txt#L169-L176)

I tested this recently and it works. However, I tested only on linux.

P.S.: If you don’t have a SuperBuild, I advise to add it if you want to use this approach.
