1

I am using grass 7.8.4. I'm trying to run some simple commands using the Grass Python Scripting Library in Python. For modules for which the source code is written in C++ or any other language than Python, it works without problem but for the modules written in Python it does not. Here is an example of a simple command I tried to run:

import grass.script as gscript
gscript.run_command("v.db.dropcolumn", flags="help")

It raises the error:

grass.exceptions.CalledModuleError: Module run None v.db.dropcolumn --help ended with error.
Process ended with non-zero return code 9009

I already checked and I know that return code 9009 means it does not find the python script. I solved the error by just adding ".py" in the command:

gscript.run_command("v.db.dropcolumn.py", flags="help")

I would like the command to work without the ".py" because I'm writing a python script that makes use of some addons. For the addons to work in my python script I sometimes need to modify them because there are running commands like "v.db.dropcolumn" without the ".py", so I have to add it. But then, the addons do not work anymore when I use them directly in the grass GUI. Does anybody know how to solve this?

  • 1
    Where are you running the script? Within grass GIS shell or you are calling it externally? If you try to run gscript.run_command('g.region', flags='p') does it work or does this raise an error too? – F.H. Jan 14 '21 at 18:05
  • I'm calling it externally in visual studio code. And gscript.run_command('g.region', flags='p') runs without any problem. It raises an error for all the modules written in python – Camille Morlus Jan 14 '21 at 19:33
  • Maybe this could be the problem: 'help' is not a flag. Flags are preceded by a single '-', while to call 'help' you use two --. The syntax should be: gscript.run_command("v.db.dropcolumn", help=True). Please try it and let me know if the error is still present. – F.H. Jan 14 '21 at 20:23
  • By the way, why don't you import all the pygrass modules shortcuts and call the commands directly from the code? I usually import them in my scripts. (import grass.script as gscript from grass.pygrass.modules import Module from grass.pygrass.modules.shortcuts import general as g from grass.pygrass.modules.shortcuts import vector as v from grass.pygrass.modules.shortcuts import raster as r from grass.pygrass.modules.shortcuts import imagery as ii) – F.H. Jan 14 '21 at 20:25
  • I have tried gscript.run_command("v.db.dropcolumn", help=True) and it does not change anything, I still get the same error. I don't think it is related to the help flag because for instance installing an extension produces the same error as well (when using gscript.run_command("g.extension.py", extension="v.class.mlR") for instance). I didn't use pygrass because I'm new to grass and I didn't think about it.. thank you for the tips! How would it look like to call "v.db.dropcolumn" using pygrass? Thank you for your help :) – Camille Morlus Jan 14 '21 at 20:33
  • I usually run my grass scripts from grass shell (avoid using the GUI python directly, as it hangs when you call some commands iteratively), so I wouldn't know how to guide you in the troubleshooting in other compilers, as I'm not familiar with them. I initially tried grass script from spyder, but switched immediately to calling my script (opened in an external editor, like sublime text) from the shell, as it was just simplier and faster. I suggest you to do the same thing, unless you need to use an external tool. In that case I'm afraid I couldn't be helpful – F.H. Jan 14 '21 at 20:42
  • calling the commands from the scripts is quite simple, here are some examples: r.in_gdal(input=input_map , output=current_date+'_qa' , memory=max_memory, overwrite=True), v.proj(location='Paraguay_hydrography' , mapset=mapset , input=paraguay_hydrography_shp_name, overwrite=True), v.extract(input='gis_osm_water_a_free_1' , where="name = 'Lago Ypacaraí'", output='ypacarai_lake', overwrite=True) – F.H. Jan 14 '21 at 20:44
  • I have to use multiple softwares aside grass for one implementation, it's why i have to use visual studio code unfortunately. I will try with pygrass with the example commands you sent, it seems way easier! Thank you for your tips and help! – Camille Morlus Jan 14 '21 at 21:00

2 Answers2

2

welcome to GRASS community.

Have you checked if you are setting the environmental variable correctly for GRASS modules before running the modules? If the environmental variables are set correctly (in any of the suggested way in the wiki), you should not have any issues running any of the modules externally.

There is a post with examples and relevant links on how to set environmental variables correctly in Windows 10 Connecting Python script external to Grass GIS 7 program in Windows 10?

Please double check if this is the case. Thanks.

Hasnein Tareque
  • 461
  • 2
  • 11
  • Glad that worked out Camille. Also, not that you have to do, but I guess your post above as an answer should be posted as a comment? That helps to keep this post in preferred format of this forum. Thanks. – Hasnein Tareque Jan 20 '21 at 06:19
0

Thank you Hasnein for your answer. It helped me solve the issue: it was indeed related to the environmental variables. I had not add the "C:\Program Files\GRASS GIS 7.8\scripts" into the PATH variable and now that I did everything works fine. Thanks !