[GRASS-dev] Python scripts in GRASS GIS: conditional loading of external libraries?

Ondřej Pešek pesej.ondrek at gmail.com
Fri Oct 19 12:16:08 PDT 2018


Hi,

pá 19. 10. 2018 v 17:18 odesílatel Martin Landa <landa.martin at gmail.com>
napsal:

> pá 19. 10. 2018 v 16:45 odesílatel Markus Neteler <neteler at osgeo.org>
> napsal:
> > we are currently writing a Python script which needs to import some
> > heavy external libraries.
>
> if I understand well, a lazy import could help
>
> """
> def main:
>     from mylib import xyz
>     ...
>
>    return 0
>
> if __name__ == "__main__":
>     opt, flg = grass.parser()
>     sys.exit(main())
> """
>

In my  AddOn [1], I followed exactly this approach to avoid double import
of TensorFlow.

If I remember correctly, libraries imported before the call of
`grass.parser()` are imported also during the installation through
`g.extension` because of checks or something and you really don't want to
import quite heavy TensorFlow (which is moreover printing some warnings
during the import on most of computers) making your installation slow. The
same applied to the double import when running the script.

[1]
https://trac.osgeo.org/grass/browser/grass-addons/grass7/imagery/i.ann.maskrcnn/i.ann.maskrcnn.train/i.ann.maskrcnn.train.py?rev=72730#L181
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20181019/48aa89c7/attachment.html>


More information about the grass-dev mailing list