<div dir="ltr"><div dir="ltr"><div dir="ltr"><br><br>On Fri, Oct 19, 2018 at 12:23 PM Nikos Alexandris <<a href="mailto:nik@nikosalexandris.net">nik@nikosalexandris.net</a>> wrote:<br>><br>> * Markus Neteler <<a href="mailto:neteler@osgeo.org">neteler@osgeo.org</a>> [2018-10-19 16:45:29 +0200]:</div>> ...<br><div dir="ltr">> >Now: could the import of the external library (it takes in the range<br>> >of > 2min) be conditionalized to only be read at the second "run"?<br>> ><br>> >I was searching in the g.parser code but didn't really find anything useful.<br>> >An example would also be fine (addon?).<br>> ><br>> >Or am I on the wrong track?<br><div><br></div><div>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 <br></div><div><br></div><div>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.<br></div><div><br></div><div><a href="https://trac.osgeo.org/grass/search?q=lazy+import&noquickjump=1&changeset=on">https://trac.osgeo.org/grass/search?q=lazy+import&noquickjump=1&changeset=on</a><br></div><div><br></div>> An idea is to try make use of `importlib.import_module()` along with<br>> `sys`, like:<br>><br>> ```<br>> import sys<br>> import importlib<br>> if 'xyz' not in sys.modules:<br>>     importlib.import_module('xyz')<br>> ```<br>> or better (?):<br>><br>> ```<br>> if 'xyz' not in sys.modules and 'xyz' not in dir():<br>>     importlib.import_module('xyz')<br>> ```</div><div dir="ltr"><br></div><div>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:<br></div><div><br></div><div><a href="https://lists.osgeo.org/pipermail/grass-dev/2018-October/090321.html">https://lists.osgeo.org/pipermail/grass-dev/2018-October/090321.html</a><br></div><div dir="ltr"><br></div></div></div>