[GRASS-dev] Python scripts in GRASS GIS: conditional loading of external libraries?
wenzeslaus at gmail.com
Fri Oct 19 11:05:27 PDT 2018
On Fri, Oct 19, 2018 at 12:23 PM Nikos Alexandris <nik at nikosalexandris.net>
> * Markus Neteler <neteler at 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
> >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
> 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:
> or better (?):
> if 'xyz' not in sys.modules and 'xyz' not in dir():
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).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the grass-dev