0

I am installing GRASS on linux. I have managed to download source code and compile and can now run grass from the command line to open GRASS and start a session. I am trying to run python scripts outside of the GUI, however, and keep encountering the same error:

RuntimeError: Cannot find GRASS GIS start script: /bin/grass, set the right one using the GRASSBIN environm. variable

I have tried setting GRASSBIN to the output of which grass which also didnt work, and I have run the grass --config path command as outlined in these docs: https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly#Python:_GRASS_GIS_7_with_existing_location

This still does not work. Please, what am I doing wrong? I am using Amazon Linux

EDIT:

grass is installed in $HOME output of which grass: /usr/local/bin/grass78 have also tried setting GRASSBIN to $HOME/grass/bin.x86_64-pc-linux-gnu

I have also set export GISBASE=$HOME/grass export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GISBASE/lib LOCATION is unset as this is the first time I am running script. The actual script doesnt do anything at the moment:

from grass_session import Session
from grass.script import core as gcore
from multiprocessing import Pool
# import grass.script as grass
# from grass.pygrass.modules.shortcuts import general as g

def main():


    print("working")


if __name__ == "__main__":
    main()
2
  • Please post the output of: which grass, the path to your GISDBASE and LOCATION, and the python script that you are running, which throws that error.
    – Micha
    Commented Jan 6, 2022 at 8:17
  • Ok have edited the question to include that information
    – sobmortin
    Commented Jan 6, 2022 at 12:21

1 Answer 1

1

Following the "Working with GRASS without starting it explicity" that you linked to in your question, you need to set several variables in advance, before calling from grass.script import core as gcore:

# DATA
# Here set your path to grassdata (GRASS GIS database) directory
# Probably :
gisdb = os.path.join(os.path.expanduser("~"), "grass")

# Specify (existing) location and mapset
# From your first startup of GRASS in an interactive session
location = "nc_spm_08"
mapset   = "user1"

grass7bin = "/usr/local/bin/grass78" # in your case
startcmd = [grass7bin, '--config', 'path']
p = subprocess.Popen(startcmd, shell=False,
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
    print >>sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)" % startcmd
    sys.exit(-1)
gisbase = out.strip('\n\r')

# Set GISBASE environment variable
os.environ['GISBASE'] = gisbase
# the following not needed with trunk
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
# add path to GRASS addons
home = os.path.expanduser("~")
os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons', 'scripts')

# define GRASS-Python environment
gpydir = os.path.join(gisbase, "etc", "python")
sys.path.append(gpydir)

########### DATA
# Set GISDBASE environment variable
os.environ['GISDBASE'] = gisdb

Please take note that the above assumes you have already started GRASS once in the normal, interactive way, and setup a LOCATION and MAPSET. If that is not clear, please refer to this wiki page and any of the videos here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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