3

I developed a QGIS plugin that uses third party libraries. The current situation is that the user of the plugin, has to install some Python libs into QGIS, before he/she can use my plugin. Every time a new QGIS version is installed, the used needs to install the third party libs again to use my plugin. Also, in this situation, the users have no admin rights to install the libs. They need to ask their company helpdesk to install the libs.

Is there a way to not bother the user or company helpdesk at all with installation of the third party libs I use?

nlrmcm
  • 31
  • 2

1 Answers1

2

Create a requirement.txt in your plugin with all the packages that needs to be installed. Then execute it every time the plugin is loaded. Here is an example requirement.txt file:

enter image description here

Here is how you can install the packages in plugin:

import pathlib
import sys
import os.path
def installer_func():
    plugin_dir = os.path.dirname(os.path.realpath(__file__))

    try:
        import pip
    except ImportError:
        exec(
            open(str(pathlib.Path(plugin_dir, 'scripts', 'get_pip.py'))).read()
        )
        import pip
        # just in case the included version is old
        pip.main(['install', '--upgrade', 'pip'])

    sys.path.append(plugin_dir)

    with open(os.path.join(plugin_dir,'requirements.txt'), "r") as requirements:
        for dep in requirements.readlines():
            dep = dep.strip().split("==")[0]
            try:
                __import__(dep)
            except ImportError as e:
                print("{} not available, installing".format(dep))
                pip.main(['install', dep])

Call this function in your main file. You can add a note to run QGIS as administrator in the plugin description.

sheharbano
  • 211
  • 2
  • 13