Wednesday 12 February 2020

Using Python script to control GRASS GIS from outside?


I am working with GRASS 6.4.3 and python 2.7.6 under WIN 8.1.


I am a novice with computer science and coding, and I have seen many posts including those:




Here's my python code, which wants to use the GRASS module g.list to list my raster data in GRASS. I type those code in Python IDLE.


import os
import sys

gisbase = os.environ['GISBASE'] = 'C:\OSGeo4W64\apps\grass\grass-6.4.3' #GISBASE needs to point the root of the GRASS installation directory
gisrc = 'C:\Users\Heinz\Documents\grassdata'
gisdbase = 'C:\Users\Heinz\Documents\grassdata'
location = 'newLocation'
mapset = 'TC'
LD_LIBRARY_PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib'

PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc';'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python';'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib';'C:\OSGeo4W64\apps\grass\grass-6.4.3\bin';'C:\Python27';'C:\OSGeo4W64\apps\Python27';'C:\OSGeo4W64\apps\msys'
PYTHONLIB = 'C:\Python27'
PYTHONPATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python'
GRASS_SH = 'C:\OSGeo4W64\apps\msys\bin\sh.exe'


sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))

import grass.script as grass


And I have set every path in my code as environment variable in the windows control panel.


As I run module, I got the error:


enter image description here


enter image description here


Which line of my code is wrong or not necessary and what code should I add to my python script, or can't I use Python IDLE to do this?




@ustroetz I have found no file in my computer named .grassrc6 , so I create a new file named .grassrc6(which is empty), adding a line in my code:


gisrc = 'C:\Users\Heinz\.grassrc6'

But still got the same error.



Did I misunderstand the instruction in the post?




I create a text file named grass.pth in this folder C:\OSGeo4W\apps\python27\lib\site-packages, and type those two lines in it: enter image description here


Here is the list of my environment variables I have set in windows penal:


GISBASE = C:\OSGeo4W64\apps\grass\grass-6.4.3
GISRC = C:\Users\Heinz\Documents\grassdata
Path =
C:\Python27
C:\OSGeo4W64\apps\grass\grass-6.4.3
C:\Users\Heinz\Documents\grassdata

C:\OSGeo4W64\apps\grass\grass-6.4.3\lib
C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python
C:\OSGeo4W64\apps\grass\grass-6.4.3\etc
C:\OSGeo4W64\apps\grass\grass-6.4.3\bin
C:\OSGeo4W64\apps\Python27
C:\OSGeo4W64\apps\msys
C:\OSGeo4W64\apps\grass\grass-6.4.3\scripts
C:\OSGeo4W64\bin
C:\OSGeo4W64\apps\Python27\Lib\site-packages


then I ran my python script, and got the same error: enter image description here


You can see that I have added this line:sys.path.append('C:\OSGeo4W\bin') in my script(it's because the script didn't work before I added it), but it still got the same error.




@Martin, I have found that I have three places where python installed:


C:\OSGeo4W64\apps\Python27
C:\Python27
C:\Program Files (x86)\Quantum GIS Lisboa\apps\Python27

and I also found site-packages files in those places, so I copied grass.pth to those places, but my script still can't work.


I also found this python-related file:



C:\Users\Heinz\Downloads\http%3a%2f%2fdownload.osgeo.org%2fosgeo4w%2f\x86_64\release\python

I don't know if this keeps my script useless, should I delete this file, or it doesn't matter?


I have tried this in cmd: enter image description here


I will keep trying to make my script work!



Answer



After some hard work, I got my GRASS working with Python using a .pth file in the sitepackages folder.


To try this, do to the following steps:



  1. Go to the folder C:\OSGeo4W\apps\python27\lib\site-packages


  2. Create a file called grass.pth and open it with an editor


  3. Enter the following two lines (assuming your GRASS was installed with OSGeo4W; check the paths to be sure):


    C:\OSGeo4W\apps\grass\grass-6.4.3\etc\python C:\OSGeo4W\bin




  4. Save and close the file




  5. Try the following statement in your Python Script



    import grass.script as grass




P.S.: To make that work, you have to set your enviroment variables as described in posts above.


Another option might be to add the C:\OSGeo4W\bin path to your script with sys.path.append as you did with other paths


Once it works, check out the following post for getting to run the grass tools: GRASS Geoprocessing in Python Script


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...