2

Pretty simple question that an internet search (or a few guesses in python) cant answer:

How can one programmatically determine which if any version of GRASS is available in pyQGIS

Best I could come up with was to query processing.alglist("grass") into a variable and regex the result. Pretty much what I did in an adhoc (by eye rather than regex) fashion to solve the immediate problem. But this seems silly.

An alternative idea was simply to run the desired algorithm, but that often returns nothing to the command line, as though it ran correctly. Making EAFP problematic.

Is there a simple way to query which if any version of GRASS is available?

As with @Joseph answer some information is available from grass...utils so:

grass = processing.algs.grass.Grass7Utils.GrassUtils
grass7 = processing.algs.grass7.Grass7Utils.Grass7Utils

However, I had an error with grass7.installedVersion() but grass7.getGrassVersion() worked.

I also note that there is grass7.isGrass7Installed and grass.isGrassInstalled but the results dont nessasarily match whats available if I open processing/toolbox.

For example in my QGIS2.8 Grass 6.4.0 is installed and available from processing/toolbox but grass.isinstalled returns false. In my QGIS2.18(with GRASS7) grass 7 is available from the processing/toolbox but the getversion returns both 6.4.0 and 7.0.0 for the respective version numbers and .isinstalled returns false for both.

The upshot is there appears to be multiple ways to determine the availabilty of an available grass version with little sychronicity to the reality of which version is actually available in the toolbox.

Perhaps my (windows) installed versions of QGIS are interfering with each other? I'll need to test this on my linux machine too.

2

Inside the processing directory for GRASS 7, there is a installedVersion() function within the Grass7Utils.py file (which for me resides in C:/Program Files/QGIS 2.18/apps/qgis/python/plugins/processing/algs/grass7/). You can call this function from the Python Console to see which version is installed:

import processing
processing.algs.grass7.Grass7Utils.Grass7Utils.installedVersion()

Example


If GRASS7 is not available and you want to see which GRASS version is installed instead, you could use something like:

import processing

grass7 = processing.algs.grass7.Grass7Utils.Grass7Utils
grass = processing.algs.grass.GrassUtils.GrassUtils

if grass7:
    grass7.installedVersion()
else:
    grass.getGrassVersion()
  • I had an error with grass7.installedVersion() but grass7.getGrassVersion() worked – Mr Purple Jan 18 '18 at 21:27
  • Question updated to reflect application of this answers info. cheers. – Mr Purple Jan 18 '18 at 21:46
  • @MrPurple - Interesting, I wonder why False is returned for both those functions. Might be useful to post a question on the QGIS mailing list and see if they know why this happens? – Joseph Jan 19 '18 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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