[GRASS-user] need to run python-grass script outside grass in Unix environment

Group,

I need help to run a python-grass script outside grass. I executed the following suggested program with the errors

line 1: import: command not found
/home/alassane/Programs/pythonGrass-example.py: line 2: import: command not found
/home/alassane/Programs/pythonGrass-example.py: line 3: import: command not found
/home/alassane/Programs/pythonGrass-example.py: line 6: syntax error near unexpected token (' /home/alassane/Programs/pythonGrass-example.py: line 6: gisdb = os.path.join(os.path.expanduser(“~”), “/home/alassane/'ata”)

Here is the code I copied in a file and named it grasspython.py


import os
import sys
import subprocess

define GRASS Database

add your path to grassdata (GRASS GIS database) directory

gisdb = os.path.join(os.path.expanduser(“~”), “/home/alassane/Data”)

the following path is the default path on MS Windows

gisdb = os.path.join(os.path.expanduser(“~”), “Documents/grassdata”)

specify (existing) Location and Mapset

location = “SanbornCD”
mapset = “PERMANENT”

path to the GRASS GIS launch script

we assume that the GRASS GIS start script is available and on PATH

query GRASS itself for its GISBASE

(with fixes for specific platforms)

needs to be edited by the user

grass7bin = ‘/usr/bin/grass70’
if sys.platform.startswith(‘win’):

MS Windows

grass7bin = r’C:\OSGeo4Win\grass70.bat’

uncomment when using standalone WinGRASS installer

grass7bin = r’C:\Program Files (x86)\GRASS GIS 7.0.0\grass70.bat’

this can be avoided if GRASS executable is added to PATH

elif sys.platform == ‘darwin’:

Mac OS X

TODO: this have to be checked, maybe unix way is good enough

grass7bin = ‘/Applications/GRASS/GRASS-7.0.app/’

query GRASS GIS itself for its GISBASE

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 = out.strip(os.linesep)

set GISBASE environment variable

os.environ[‘GISBASE’] = /usr/local/grass-7.0.1svn

define GRASS-Python environment

grass_pydir = os.path.join(gisbase, “etc”, “python”)
sys.path.append(grass_pydir)

import (some) GRASS Python bindings

import grass.script as gscript

launch session

rcfile = gscript.setup.init(gisbase, gisdb, location, mapset)

example calls

gscript.message(‘Current GRASS GIS 7 environment:’)
print gscript.gisenv()

gscript.message(‘Available raster maps:’)
for rast in gscript.list_strings(type=‘raster’):
print rast

gscript.message(‘Available vector maps:’)
for vect in gscript.list_strings(type=‘vector’):
print vect

delete the rcfile

os.remove(rcfile)


Obviously i am new with this and your help is appreciated.

Thanks

Alassane

On Mon, May 30, 2016 at 6:45 PM, alassane toure <atourej@gmail.com> wrote:

I need help to run a python-grass script outside grass. I executed the
following suggested program with the errors

line 1: import: command not found
/home/alassane/Programs/pythonGrass-example.py: line 2: import: command
not found
/home/alassane/Programs/pythonGrass-example.py: line 3: import: command
not found

Hi, this sounds like you are running Python script with an interpreter like
Bash. Look at shebang. Taking example from GRASS source code,
scripts/r.shade/r.shade.py starts with:

#!/usr/bin/env python

Vaclav

[returning the conversation to the mailing list]

On Tue, May 31, 2016 at 7:35 PM, alassane toure <atourej@gmail.com> wrote:

I am having problem with the launch session...

# launch session
rcfile = gscript.setup.init(gisbase, gisdb, location, mapset)

See the system response below..

/Programs/grass/rshade$ /home/alassane/Programs/grass/rshade/testing.py
Traceback (most recent call last):
  File "/home/alassane/Programs/grass/rshade/testing.py", line 70, in
<module>
    rcfile = gscript.setup.init(gisbase, gisdb, location, mapset)
AttributeError: 'module' object has no attribute 'setup'

The example was wrong. The `__init__.py` of `script` does not `import
setup`:

https://trac.osgeo.org/grass/browser/grass/trunk/lib/python/script/__init__.py

So, the import needs to be done explicitly:

import grass.script.setup as gsetup
rcfile = gsetup.init(gisbase, gisdb, location, mapset)

The documentation fixed in:

https://trac.osgeo.org/grass/changeset/68547

Please test.

Vaclav