I try to use GRASSGIS via Python3. But when I import grass_session, Python gives out an error message: "RuntimeError: Cannot find GRASS GIS start script: C:/Program Files/GRASS GIS 7.8, set the right one using the GRASSBIN environm. variable"

Here's my code:

import os
import sys
import subprocess

grass7bin = r'C:/Program Files/GRASS GIS 7.8/grass78.bat'
startcmd = [grass7bin, '--config', 'path']
try:
    p = subprocess.Popen(startcmd, shell=False,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
except OSError as error:
    sys.exit("ERROR: Cannot find GRASS GIS start script"
             " {cmd}: {error}".format(cmd=startcmd[0], error=error))
if p.returncode != 0:
    sys.exit("ERROR: Issues running GRASS GIS start script"
             " {cmd}: {error}"
             .format(cmd=' '.join(startcmd), error=err))
gisbase = r'C:/Program Files/GRASS GIS 7.8'

os.environ['GISBASE'] = gisbase
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)

import grass.script.setup as gsetup

gisdb = os.path.join(os.path.expanduser("~"), "C:/Users/user/Documents/grassdata")
location = "Testlocation"
mapset = "test"

rcfile = gsetup.init(gisbase, gisdb, location, mapset)

# use grass functions here

os.remove(rcfile)

import grass_session as session

What do I wrong?

0

There are 0 best solutions below