[GRASS-user] Envoking GRASS in a python script

kungphil phil.wilkes at dse.vic.gov.au
Tue Oct 18 02:05:44 EDT 2011


Hello

I am trying to envoke GRASS 6.4.1 from a python(2.6) script on a Solaris 10
OS, I am creating the WIND, PROJ_INFO and PROJ_UNITS from a PSQL database
table and this seems to work fine as it creates a vaible GRASS session.  The
next seesion is the part I am havin a problem with, please see script
below...

Import os, sys, psycopg2, getpass, shutil, shlex

user = getpass.getuser()

#Create temporary GRASS directories
tLOCATION = ('/home/syst/' + user + '/tempGRASS')
os.mkdir(tLOCATION)
os.mkdir(tLOCATION + '/PERMANENT')

#CONNECT TO DB
conn = psycopg2.connect("dbname=statewide user=" + user)
cur = conn.cursor()

list = ['WIND', 'PROJ_INFO', 'PROJ_UNITS']
for item in list:
        cur.execute('select val from grass_info where file = \'' + item +
'\';')
        sGrassPram = str(cur.fetchall()[0])
        sGrassPram =
sGrassPram[sGrassPram.find('(')+2:sGrassPram.find(')')-2]
        aFILE = open(tLOCATION + '/PERMANENT/' + item,'w')
        for file in str.split(sGrassPram,','):
                file =  str.strip(file) + '\n'
                aFILE.write(file)
        aFILE.close()
shutil.copyfile(tLOCATION + '/PERMANENT/WIND', tLOCATION +
'/PERMANENT/DEFAULT_WIND')

#CREATE GRASS RC
aFILE = open(tLOCATION + '/PERMANENT/GRASSRC','w')
aFILE.write('GISDBASE:  ' + tLOCATION + '\n')
aFILE.write('LOCATION_NAME:     tempGRASS' + '\n')
aFILE.write('MAPSET:    PERMANENT')
aFILE.close()

#GRASS environment variables
os.environ['PYTHONPATH'] = '$PYTHONPATH:/opt/isg/grass-6.4.1-64/etc/python'
os.environ['GISBASE'] = '/opt/isg/grass-6.4.1-64'
os.environ['PATH'] = '$PATH:$GISBASE/bin:$GISBASE/scripts'
os.environ['LD_LIBRARY_PATH'] = '$LD_LIBRARY_PATH:$GISBASE/lib'
os.environ['GIS_LOCK'] = '$$'
os.environ['GISRC'] = tLOCATION + '/PERMANENT/GRASSRC'

try:
        import grass.script as grass
        env = grass.gisenv()
        base = env['GISDBASE']
        print base
except Exception, err:
        print err

Doing this I get an error (with traceback)...

Traceback (most recent call last):
  File "/scratch/pw38/sql_py_grass/grass_temp.py", line 44, in <module>
    env = grass.gisenv()
  File "/opt/isg/grass-6.4.1-64/etc/python/grass/script/core.py", line 514,
in gisenv
    s = read_command("g.gisenv", flags='n')
  File "/opt/isg/grass-6.4.1-64/etc/python/grass/script/core.py", line 225,
in read_command
    ps = pipe_command(*args, **kwargs)
  File "/opt/isg/grass-6.4.1-64/etc/python/grass/script/core.py", line 202,
in pipe_command
    return start_command(*args, **kwargs)
  File "/opt/isg/grass-6.4.1-64/etc/python/grass/script/core.py", line 164,
in start_command
    return Popen(args, **popts)
  File "/opt/isg/grass-6.4.1-64/etc/python/grass/script/core.py", line 55,
in __init__
    startupinfo, creationflags)
  File "/opt/isg/lib/python2.6/subprocess.py", line 621, in __init__
    errread, errwrite)
  File "/opt/isg/lib/python2.6/subprocess.py", line 1126, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Any help appreciated

Regards, Phil

As an aside I don't think the os.environ is working correctly, if I comment
out the 'import grass.script as grass' and below and then run the script I
get no error (then get the error above if I include it again), but if I run
the above script in a new shell I get the error...

No module named grass.script

Help with this would also be appreciated or should I start new thread?


--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Envoking-GRASS-in-a-python-script-tp6903363p6903363.html
Sent from the Grass - Users mailing list archive at Nabble.com.


More information about the grass-user mailing list