# I wrote a python extension, how to automatically install the extension by python?

**URL:** <https://discourse.slicer.org/t/i-wrote-a-python-extension-how-to-automatically-install-the-extension-by-python/29002>\
**Category:** Development\
**Tags:** extensions-manager, python\
**Created:** [April 19, 2023, 9:03am UTC](https://discourse.slicer.org/t/i-wrote-a-python-extension-how-to-automatically-install-the-extension-by-python/29002 "2023-04-19T09:03:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)\
**Post date:** [April 19, 2023, 9:03am UTC](https://discourse.slicer.org/t/i-wrote-a-python-extension-how-to-automatically-install-the-extension-by-python/29002/1 "2023-04-19T09:03:54Z")

</div>

Below seems not to work.

```auto
import slicer
import os

# Path to your extension .py file
extension_file = "/path/to/YourExtension.py"

# Check if the extension is already installed
installed_extensions = slicer.app.extensionsManagerModel().installedExtensions()
if any(ext.name == "YourExtension" for ext in installed_extensions):
    print("YourExtension is already installed.")
else:
    # Install the extension
    slicer.app.extensionsManagerModel().installExtension(extension_file)
    print("YourExtension has been installed.")

# Enable the extension (optional)
extension_metadata = slicer.app.extensionsManagerModel().retrieveExtensionMetadataByName("YourExtension")
if extension_metadata:
    extension_id = extension_metadata["id"]
    slicer.app.extensionsManagerModel().setExtensionEnabled(extension_id, True)
    print("YourExtension has been enabled.")

```

---

<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:** [April 20, 2023, 4:32am UTC](https://discourse.slicer.org/t/i-wrote-a-python-extension-how-to-automatically-install-the-extension-by-python/29002/2 "2023-04-20T04:32:58Z")

</div>

If you don’t want to build an extension package (because that would require you to build Slicer, or you need to carefully build an extension package zip file manually), then probably the simplest is to copy your .py file into a scripted module folder in Slicer.

If you want to keep your code better separated from Slicer then you can load it on-the-fly or add it to the additional module paths using Python scripting as it is shown [here](https://github.com/Slicer/Slicer/blob/d1e0b86069aa6926b13c5aaf6f649577c75c1a00/Modules/Scripted/ExtensionWizard/ExtensionWizard.py#L357-L408).

In recent Slicer Preview Releases you can just drag-and-drop the .py file to the application window to load/install it.

---

<div class="post-metadata">

**Author:** ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)\
**Post date:** [April 20, 2023, 4:37am UTC](https://discourse.slicer.org/t/i-wrote-a-python-extension-how-to-automatically-install-the-extension-by-python/29002/3 "2023-04-20T04:37:27Z")

</div>

Thank you for the reply! It is installed through settings–Modules–Additional module paths. For this kind of Python extension, is it possible to install automatically by Python script? Thanks a lot!
