1

I'm trying very hard to get spyder to run the grass module but have not succeeded.

I tried the grass documentation and these Q&As:

  1. Using Spyder IDE to develop python for Grass GIS, get 'ImportError: No module named grass.script' and
  2. How do I connect my Python script external to the Grass GIS 7 program in Windows 10?

This is my code:

import os
import sys
import subprocess

grass7bin = r'C:\OSGeo4W64\bin\grass72.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 = str(out.decode('utf-8'))
gisbase = gisbase.rstrip()
os.environ['GISBASE'] = gisbase

#set up GRASS environment variables
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)

import grass.script.setup as gsetup

I ran spyder with python 3 and grass 7.2.0.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
asher
  • 43
  • 4

0 Answers0