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?

9
  • 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, 2021 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 Jan 14, 2021 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, 2021 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, 2021 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 :) Jan 14, 2021 at 20:33

2 Answers 2

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.

1
  • 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. Jan 20, 2021 at 6: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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.