[GRASS-dev] Addons: Python error when checking if package is installed - r.randomforest

Steven Pawley dr.stevenpawley at gmail.com
Mon Jun 27 08:03:05 PDT 2016

Thanks for this Vaclav. I'm confused as to why sklearn is not being found
by testing for the presence of the module, but I can make an update to use
the try-except method to get around this.

On Mon, Jun 27, 2016 at 8:00 AM, Vaclav Petras <wenzeslaus at gmail.com> wrote:

> On Mon, Jun 27, 2016 at 7:18 AM, Markus Neteler <neteler at osgeo.org> wrote:
>> (I noted that various GRASS scripts use different ways for this check,
>> the test for 3rd party packages should be standardized.)
> I so far promoted lazy import* as a solution to these dependencies. The
> important part is that import happens after calling grass.script.parser(),
> so for manual and GUI, you don't need to have the dependencies at all. For
> simple modules (with dependency usage limited to main() function), the
> import** is done in main() after parser() call and try-except with
> ImportError is used to catch the missing dependency and report an error to
> the user (example: r.colors.cubehelix***). More complicated modules (with
> explicit dependency usage outside of main() function) must be solved on a
> case-to-case basis (examples: v.class.ml and v.class.mlpy).
> Older discussions:
> https://lists.osgeo.org/pipermail/grass-dev/2015-February/073734.html
> https://lists.osgeo.org/pipermail/grass-dev/2016-March/079610.html
> Examples:
> https://trac.osgeo.org/grass/browser/grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.py#L186
> https://trac.osgeo.org/grass/browser/grass-addons/grass7/raster/r.flexure/r.flexure.py?rev=64452
> https://trac.osgeo.org/grass/changeset/66482/
> Vaclav
> * By lazy import I mean an import which is not done at the beginning of
> the file, but somewhere in some function only right before it is actually
> needed.
> ** I think that import with try-except is just more straightforward than
> testing the presence of the module/package before the import. It also
> follows "it is easier to ask for forgiveness than for permission" (EAFP),
> so one can say that it is more Pythonic.
> *** r.colors.cubehelix is actually not the basic example, because it
> provides fallback when the dependency is not available and shows just
> warning and an error but without exiting (uses warning() and error() and it
> does not use not fatal()).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20160627/d98457b7/attachment.html>

More information about the grass-dev mailing list