<div dir="ltr"><div><div><div><div><div>Group,<br></div>I need help to run a python-grass script outside grass.  I executed the following suggested program with the errors <br><br>line 1: import: command not found<br>/home/alassane/Programs/pythonGrass-example.py: line 2: import: command not found<br>/home/alassane/Programs/pythonGrass-example.py: line 3: import: command not found<br>/home/alassane/Programs/pythonGrass-example.py: line 6: syntax error near unexpected token `('<br>/home/alassane/Programs/pythonGrass-example.py: line 6: `gisdb = os.path.join(os.path.expanduser("~"), "/home/alassane/'ata")<br><br><br></div>Here is the code I copied in a file and named it grasspython.py<br><br>*************************************************************<br>import os
<br>import sys
<br>import subprocess
<br># define GRASS Database
<br># add your path to grassdata (GRASS GIS database) directory
<br>gisdb = os.path.join(os.path.expanduser("~"), "/home/alassane/Data")
<br># the following path is the default path on MS Windows
<br># gisdb = os.path.join(os.path.expanduser("~"), "Documents/grassdata")
<br> <br># specify (existing) Location and Mapset
<br>location = "SanbornCD"
<br>mapset = "PERMANENT"
<br> <br># path to the GRASS GIS launch script
<br># we assume that the GRASS GIS start script is available and on PATH
<br># query GRASS itself for its GISBASE
<br># (with fixes for specific platforms)
<br># needs to be edited by the user
<br>grass7bin = '/usr/bin/grass70'
<br>if sys.platform.startswith('win'):
<br>    # MS Windows
<br>    grass7bin = r'C:\OSGeo4Win\grass70.bat'
<br>    # uncomment when using standalone WinGRASS installer
<br>    # grass7bin = r'C:\Program Files (x86)\GRASS GIS 7.0.0\grass70.bat'
<br>    # this can be avoided if GRASS executable is added to PATH
<br>elif sys.platform == 'darwin':
<br>    # Mac OS X
<br>    # TODO: this have to be checked, maybe unix way is good enough
<br>    grass7bin = '/Applications/GRASS/GRASS-7.0.app/'
<br> <br># query GRASS GIS itself for its GISBASE
<br>startcmd = [grass7bin, '--config', 'path']
<br>try:
<br>    p = subprocess.Popen(startcmd, shell=False,
<br>                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
<br>    out, err = p.communicate()
<br>except OSError as error:
<br>    sys.exit("ERROR: Cannot find GRASS GIS start script"
<br>             " {cmd}: {error}".format(cmd=startcmd[0], error=error))
<br>if p.returncode != 0:
<br>    sys.exit("ERROR: Issues running GRASS GIS start script"
<br>             " {cmd}: {error}"
<br>             .format(cmd=' '.join(startcmd), error=err))
<br>gisbase = out.strip(os.linesep)
<br> <br># set GISBASE environment variable
<br>os.environ['GISBASE'] = /usr/local/grass-7.0.1svn
<br> <br># define GRASS-Python environment
<br>grass_pydir = os.path.join(gisbase, "etc", "python")
<br>sys.path.append(grass_pydir)
<br> <br># import (some) GRASS Python bindings
<br>import grass.script as gscript
<br> <br># launch session
<br>rcfile = gscript.setup.init(gisbase, gisdb, location, mapset)
<br> <br># example calls
<br>gscript.message('Current GRASS GIS 7 environment:')
<br>print gscript.gisenv()
<br> <br>gscript.message('Available raster maps:')
<br>for rast in gscript.list_strings(type='raster'):
<br>    print rast
<br> <br>gscript.message('Available vector maps:')
<br>for vect in gscript.list_strings(type='vector'):
<br>    print vect
<br> <br># delete the rcfile
<br>os.remove(rcfile)<br><br>*****************************************************************************<br><br></div>Obviously i am new with this and your help is appreciated.<br><br></div>Thanks<br><br></div>Alassane<br></div>