# How to use a python package in an extension?

**URL:** <https://discourse.slicer.org/t/how-to-use-a-python-package-in-an-extension/12736>\
**Category:** Support\
**Created:** [July 26, 2020, 10:53pm UTC](https://discourse.slicer.org/t/how-to-use-a-python-package-in-an-extension/12736 "2020-07-26T22:53:32Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [July 26, 2020, 11:18pm UTC](https://discourse.slicer.org/t/how-to-use-a-python-package-in-an-extension/12736/2 "2020-07-26T23:18:29Z")

</div>

I think these two posts will help you out! Which python package on PyPI are you looking to use in your extension?

Simplest (Really only recommended for packaged on PyPI that have wheels available):

> [@Install python library with extension](https://discourse.slicer.org/t/install-python-library-with-extension/10110/2):
>
> 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

More involved, but gets the external python package at time of building the extension and then the files are packaged together when building the extension package file:

> [@Install python library with extension](https://discourse.slicer.org/t/install-python-library-with-extension/10110/8):
>
> 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): 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.…](https://github.com/Punzo/SlicerAstro/blob/master/SuperBuild/External_python-slicerastro-requirements.cmake)

---

_[View the full topic](https://discourse.slicer.org/t/how-to-use-a-python-package-in-an-extension/12736)._
