0

I want to call some grass modules(e.g. r.walk module) in my python script. And fortunately I find lots of useful hints on https://grasswiki.osgeo.org/wiki/GRASS_and_Python#MS-Windows. Here it is.

MS-Windows In order to use GRASS functionality via Python from outside, some environment variables have to be set:
GISBASE= C:\GRASS-64 GISRC= C:\Documents and Settings\user\.grassrc6 LD_LIBRARY_PATH= C:\GRASS-64\lib PATH= C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26; PYTHONLIB= C:\Python26 PYTHONPATH= C:\GRASS-64\etc\python GRASS_SH= C:\GRASS-64\msys\bin\sh.exe

However, msys folder is nonexistent after I install the latest version WinGRASS-7.0.3 and 7.0.4. What I want to know is how to set the environment variables using the latest version when there is no msys folder. Thanks a lot.

fly
  • 1
  • 2

1 Answers1

1

Better guide then the wiki is in the official documentation:

You just need to specify (the path to) your GRASS binary. That's something like grass70 on Linux, /Applications/GRASS/GRASS-7.0.app/ on Mac and C:\OSGeo4Win\grass70.bat on MS Windows. In general it depends on how you installed GRASS GIS. On MS Windows watch for the difference between OSGeo4W install and standalone installer. Each of them will install GRASS to different directories (you also specify the directories during the installation wizard).

The boilerplate code uses subprocess.Popen to call the GRASS binary with --config path to get the values needed to set the parameters. Then it adds extends PYTHONPATH (sys.path) with .../etc/python in GRASS installation. Finally, grass.script.setup.init is called which sets up the remaining runtime variables and it also connects the GRASS session to the GRASS Database, Location and Mapset which looks like this:

import grass.script.setup as gsetup
rcfile = gsetup.init(gisbase, gisdb, location, mapset)
# ... do arbitrary grass
os.remove(rcfile)
wenzeslaus
  • 667
  • 6
  • 13
  • Thanks. I made it. – fly May 16 '16 at 13:16
  • @fly, you are welcome. But please note that the StackExchange forums works in a way that you accept the answer if it solves the problem for you and you upvote it if you think that it is a good one. Then it is clear which questions are answered and which answers are good. That's what makes SE special and different from other forums where you need to read the whole discussion to see what is the best answer or solution. – wenzeslaus Jun 15 '16 at 15:45