4

I've been working on a plugin for use with QGIS 3.16. I have developed it locally and I want to now deploy it so others can use it. The problem or rather question I'm having is: How do I deal with dependencies on external packages?

I found this answer: Development of a plugin which depends on an external Python library

and this discussion: http://osgeo-org.1560.x6.nabble.com/A-pipinstall-plugin-is-possible-First-What-s-the-difference-between-the-Osgeo4w-Shell-td5107633.html

But I'm wondering how people handle this right now. I'm pretty new to Python and QGIS both so maybe my concerns just stem from not being informed. Some of the packages I had to install are a little tricky to get working on Windows (geopandas for example), which is the first part I'm not sure how to handle. I've read that installing with conda seems to work reliably, but that doesn't seem to be a viable option since I have no idea whether a user will have conda installed (or am I missing something here?).

I'm basically looking for some documentation/recommendations to handle dependencies semi-gracefully. I would like to avoid changing the users setup if possible, since my plugin might need a different version of some package and another plugin might require a different version. Any tips/good reads on how to go about this?

Is it possible to have the plugin in its own virtual environment for example? Or can I somehow download the necessary packages and include them from a file? For geopandas, that would probably mean also downloading its dependencies, can I then force geopandas to use those versions of packages instead of what might be on the users path? Would you recommend such an approach?

I'm working on Windows by the way, and the plugin does not have to be cross-platform at least for now.

I realize this is a pretty broad question, but I'm hoping to get some tips on where to start looking for a solution.

2 Answers 2

1

QGIS has written standards for handling external dependencies - they require that the dependencies are clearly stated in the About metadata field. You can include a short guide for installing the python libraries as needed, or link to existing guides. They also suggest that if the dependencies are not available in OSGeo4w Python, that you provide instructions on how to install them on Windows. If your plugin requires libraries with different versions than what the user has, it will be up to the user to create a virtual environment with the correct dependencies and versions.

If you were writing a standalone piece of software, you could package all of your external dependencies as part of the final executable file when you freeze your python application. There are tools like PyInstaller that handle all of the external dependencies so that the user doesn't have to install anything else to make your executable work.

source: https://plugins.qgis.org/publish/

1

It's good to know (from @Michael Ward) that explaining the dependencies in the metadata.txt (in the About field) is part of the written standard.

What we have also done is include instructions in the About field for how to install the dependencies. Installing dependencies into the proper Python that QGIS uses can be quite complex, what works on Windows won't work on Mac, and what works in one version of QGIS might not work in another, because the Python that QGIS uses can be hard to find. Users, even advanced users, have a hard time figuring out how to properly install the Python dependencies into the Python that QGIS is using on their machine.

We have found lately that almost all users can install modules into the correct Python by invoking pip from the QGIS Python Console. If the user is not an administrator, pip will install the module somewhere different than where QGIS will look for it, so the user will have to amend the sys.path. Our instructions look something like this:


Before using this plugin, you can use the following method to install the dependencies. Open the QGIS menu and pick: 'Plugins | Python Console' Then enter the following:

import pip
pip.main(["install","package_1", "package_2"])

If you are not an admin user, pip might not install the package in the correct location. Pip will explain the error, and tell you where it installed your package. Then, you will need to add that location to QGIS's list of places to look for Python modules. Again in the Python Console, type:

import sys
sys.path.append(“/path/to/dir”)

Replace /path/to/dir with the installation location reported by pip.

Now, check that the packages were installed by typing in the Python Console:

import package_1
import package_2

A further wrinkle is that sometimes the Python installation that QGIS is using doesn't have pip. In that case, you could include get-pip.py in your distribution, and instruct the user to type the commands in the Python console to run the get-pip.py program, to install pip, before they try to use pip to install the dependencies. But, in our experience, this is now something that gets deep in to The Python Problem, beyond the skill level and tolerance of a typical QGIS user. It might be better to invite them to install a more recent version of QGIS, or buy a new computer, or phone a Python expert friend, rather than trying to get them to run get-pip.py from the Python Console.

Anyway, that's what we've done, and it seems to be working out ok nowadays.

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.