[QGIS-Developer] QGIS plugin depends on pypi package

Julien Moura julien.moura at oslandia.com
Fri Jan 19 08:48:02 PST 2024


Hello John,


It's a recurring issue/question since QGIS does not support pip 
dependencies and that PyQGIS plugins are not following the official 
Python packaging standards from PEPs and related formats (typically 
metadata.txt instead of setup.cfg, pyproject.toml). You will find some 
unsuccessful QEPs about this. For example: 
https://github.com/qgis/QGIS-Enhancement-Proposals/issues/202.


Shortly, here come 3 workarounds:

  * basic: run pip install in a rough subcommand. Not so elegant but
    still efficient.
  * re-wheeling: implement a minimalist (or complete) 3rd party packages
    managers, miming pip through QgsNetwork tooling. Best example I know
    until today: ENMap-Box plugin (see attached screenshot or try it
    yourself).
  * YAML addicts: run your pip install during your plugin's packaging
    job in CI and include external dependencies in a subfolder before
    deploying it to your favorite plugins repository. Then, manage
    install loading from your plugin with try/except ImportError and
    'site' module. This is the way I prefer and use for years now. Main
    downside: for dependencies requiring a specific compilation on an
    operating system, you need to include various outputs. See Roof
    Classify <https://github.com/DINFO-UniFI/RoofClassify> (GitHub
    Workflow), Thyrsis <https://gitlab.com/Oslandia/qgis/thyrsis>
    (GitLab CI), QSoccer <https://gitlab.com/Oslandia/qgis/QSoccer/>
    (GitLab CI), QDuckDB <https://gitlab.com/Oslandia/qgis/qduckdb>
    (GitLab CI)... If you're interested, let me know. It could motivate
    me to write a tutorial detailing this method.
  * bravehearts: working on packaging your dependencies into osgeo4w;
    but they probably won't be included in default QGIS packages (.msi)

Just a quick advice: if you do some pip install within your plugin, 
during the packaging or during the run, make it under a custom folder 
related only to your plugin and use site to load extra dependencies in 
the QGIS Python environment to avoid issues.


Good luck!

Regards

Julien


Le 19/01/2024 à 16:00, John Lindsay via QGIS-Developer a écrit :
> Hello everyone,
>
> I am developing a new QGIS plugin 
> (https://plugins.qgis.org/plugins/whitebox_workflows_for_qgis/#plugin-manage) 
> that serves as a frontend for a Python geospatial analysis library 
> called Whitebox Workflows for Python (WbW). The plugin seems to work 
> well with on major exception, it requires that the user have WbW 
> installed. It can be installed quite easily using pip (pip install 
> whitebox-workflows), however, I know that QGIS uses its own localized 
> version of Python, which seems to lack a terminal app access (at least 
> on MacOS). Besides, a better solution would be for the plugin to 
> install this package via pip when it is itself first installed and 
> launched. I tried to accomplish this with the following code:
>
> https://github.com/jblindsay/whitebox_workflows_for_qgis/blob/4e463262f6cfd1987afa2f3f7e0e79656eb8d27e/whitebox_workflows_for_qgis_provider.py#L56 
> <https://github.com/jblindsay/whitebox_workflows_for_qgis/blob/4e463262f6cfd1987afa2f3f7e0e79656eb8d27e/whitebox_workflows_for_qgis_provider.py#L56>
>
> This code, which runs whenever the plugin is launched, first checks to 
> see if whitebox_workflows can be imported and if not, it runs pip from 
> the Python script to install it. Unfortunately, some users have 
> reported issues with this not working correctly:
>
> https://github.com/jblindsay/whitebox_workflows_for_qgis/issues/1 
> <https://github.com/jblindsay/whitebox_workflows_for_qgis/issues/1>
>> <https://github.com/jblindsay/whitebox_workflows_for_qgis/issues/1>
> 	
> Installation in QGis fails · Issue #1 · 
> jblindsay/whitebox_workflows_for_qgis 
> <https://github.com/jblindsay/whitebox_workflows_for_qgis/issues/1>
> AttributeError: 'NoneType' object has no attribute 'write' Traceback 
> (most recent call last): File 
> "C:\\Users/thomas.struller/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python/plugins\\whitebox_work...
> github.com
>
>
> So now I'm left trying to figure out what the recommended approach to 
> installing QGIS plugin dependencies is. Any advice from anyone that 
> has successfully dealt with this problem previously? Thanks.
>
> Regards,
>
> John Lindsay
>
>
> Prof. John Lindsay
>
> Dept. of Geography, Environment & Geomatics,
>
> The University of Guelph,
>
> Room 122, Hutt Building,
>
> E-mail: jlindsay at uoguelph.ca
>
> Phone: (519) 824-4120 ext. 56074
>
> Web: https://geg.uoguelph.ca/faculty/lindsay-john
>
> Research page: https://jblindsay.github.io/ghrg/index.html
>
> WhiteboxTools GIS project: https://www.whiteboxgeo.com/
>
> Twitter: https://twitter.com/whiteboxgeo
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info:https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20240119/858449bd/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Capture d??cran du 2024-01-19 17-39-26.png
Type: image/png
Size: 267116 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20240119/858449bd/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: julien_moura.vcf
Type: text/vcard
Size: 351 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20240119/858449bd/attachment-0001.vcf>


More information about the QGIS-Developer mailing list