4

Is it possible to install QGIS Plugins with Python code?

I would like to be able to provide users who have a basic QGIS image served up to them, to be able to quickly install all their required plugins with a tailored Python script to save them manually installing each via the usual menu route, one at a time.

I cannot see any code that runs in logs after a successful plugin install and no clues online in my searches.

7
  • 2
    you could just unpack a zip file in the packages folder
    – Ian Turton
    Jun 26, 2020 at 9:36
  • Thanks - unfortunately, there is no access to the install area as a user so cannot refer to specific locations that would typically be on a C drive. Does that make sense. Installing with QGIS via the usual route is fine, but I don't have access to do things like that unfortunately. I was hoping I might be able to have the users run their own little Python snippet to set up favourites (done) and add Plugins in one quick hit.
    – Graeme
    Jun 26, 2020 at 10:00
  • 2
    I guess there are several ways to do this. If you want to install public plugins (registered in the QGIS plugin repo), then you could use the pyplugin_installer instance. Quick example: import pyplugin_installer | pyplugin_installer.instance().fetchAvailablePlugins(False) | pyplugin_installer.instance().installPlugin('asistente_ladm_col') You can read the installer.py code to get some inspiration. Jun 26, 2020 at 12:02
  • Thank you Germán - that is exactly what I needed,
    – Graeme
    Jun 26, 2020 at 13:22
  • If you don't mind, I'll elaborate more for answering this question, as it can be the reference for other people searching how to install a plugin via Python. I'll cover more cases. Jun 26, 2020 at 16:51

1 Answer 1

8

The answer is yes.

Installing a plugin from the QGIS Plugins Repository

This is how you can install a plugin called loadthemall:

import pyplugin_installer
pyplugin_installer.instance().fetchAvailablePlugins(False)
pyplugin_installer.instance().installPlugin('loadthemall')

Plugin keys correspond to their folder names. Some examples: AppendFeaturesToLayer, asistente_ladm_col, loadthemall, profiletool.

As last resort, you could get plugin keys in this way (if you haven't called fetchAvailablePlugins, it will give you installed plugin keys):

pyplugin_installer.installer_data.plugins.all().keys()

Installing a plugin from ZIP file

pyplugin_installer.instance().installFromZipFile('/path/to/plugin/file.zip')

Uninstalling a plugin

pyplugin_installer.instance().uninstallPlugin('loadthemall')

Note: This is not a stable API, and might be modified with no backwards compatibility.

3
  • This fully solves my issues, and this in particular has helped with the issue of how to call the individual Plugins: pyplugin_installer.installer_data.plugins.all().keys()
    – Graeme
    Jun 29, 2020 at 8:45
  • What does this line do? pyplugin_installer.instance().fetchAvailablePlugins(False) Jun 19, 2021 at 13:19
  • It goes to internet and fetches data of plugins that QGIS hasn't fetched yet. If you have other repos configured, it will visit those repos as well. See the source for details. Jun 19, 2021 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.