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.

3
  • Can you elaborate on "...but have not succeeded."? Any error message?
    – markusN
    Commented Jul 22, 2017 at 9:50
  • The error message is the one in the subject line: No module named grass.script
    – asher
    Commented Jul 23, 2017 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
    Commented Jun 20, 2018 at 8:06

0

Your Answer

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

Browse other questions tagged or ask your own question.