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']
    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")

import grass.script.setup as gsetup

I ran spyder with python 3 and grass 7.2.0.

  • Can you elaborate on "...but have not succeeded."? Any error message? – markusN Jul 22 '17 at 9:50
  • The error message is the one in the subject line: No module named grass.script – asher Jul 23 '17 at 6:11
  • Did you ever get to solve that problem? I have the same problem in python 3, can't find a solution – Olivier Jun 20 '18 at 8:06

Your Answer

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

Browse other questions tagged or ask your own question.