On Fri, Oct 19, 2018 at 12:23 PM Nikos Alexandris <n...@nikosalexandris.net>
wrote:
>
> * Markus Neteler <nete...@osgeo.org> [2018-10-19 16:45:29 +0200]:
> ...
> >Now: could the import of the external library (it takes in the range
> >of > 2min) be conditionalized to only be read at the second "run"?
> >
> >I was searching in the g.parser code but didn't really find anything
useful.
> >An example would also be fine (addon?).
> >
> >Or am I on the wrong track?

This is a Python thing, so g.parser doesn't know anything about that. As
Martin suggests, any import happening after the grass.script.parser()
function call are good and lazy enough (i.e. after the

There is many examples of this already. Especially relevant examples are in
addons where the point is exactly to avoid loading the dependency
altogether when only parser is called to generate the manual page but the
build server does not have all the dependencies required by the specific
addon module.

https://trac.osgeo.org/grass/search?q=lazy+import&noquickjump=1&changeset=on

> An idea is to try make use of `importlib.import_module()` along with
> `sys`, like:
>
> ```
> import sys
> import importlib
> if 'xyz' not in sys.modules:
>     importlib.import_module('xyz')
> ```
> or better (?):
>
> ```
> if 'xyz' not in sys.modules and 'xyz' not in dir():
>     importlib.import_module('xyz')
> ```

Nikos, this is a valid approach for import magics but an overkill for this.
Python import statement can be anywhere and is sufficient for most cases.
importlib is for things like dynamic loading (e.g. plugins to GUI).
Martin's answer:

https://lists.osgeo.org/pipermail/grass-dev/2018-October/090321.html
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

Reply via email to