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:
- Go to the folder
C:\OSGeo4W\apps\python27\lib\site-packages
- Create a file called
grass.pth
and open it with an editor
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
Save and close the file
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
The linked says you need to set environmental variables. This is done in Python using e.g.:
import os
os.environ['GISBASE'] = "/some/path"
The syntax
PYTHONPATH = 'C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python'
in Python just sets a variable (global one in your case) which is just a Python variable, nothing more, nothing less. The aforementioned wiki just lists the environmental variables to be set. How you set them is up to you. You can do it ahead in the command line before starting Python, you can change it in the system (search the Internet for how to do it in some GUI for your version of MS Windows), or you can use Python (os.environ
).
Here is an example how to setup the environment (simplified from GRASS Python doc):
import os
import sys
import subprocess
grass7bin = r'C:\Program Files\GRASS GIS 7.0.4\grass70.bat'
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)
os.environ['GISBASE'] = gisbase
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)
import grass.script.setup as gsetup
gisdb = os.path.join(os.path.expanduser("~"), "Documents/grassdata")
location = "nc_spm_08"
mapset = "user1"
rcfile = gsetup.init(gisbase, gisdb, location, mapset)
# use grass functions here
os.remove(rcfile)
Now you put your GRASS module calls and GRASS Python API function calls to the place where # use grass functions here
is.
Note that you don't do anything from the above if you just run the code from inside GRASS GIS session. However, the code once you have the environment done in the right way is the same and looks for example like this:
import grass.script as gscript
gscript.run_command('g.region', s=10, n=50, w=30, e=60, res=2)
gscript.mapcalc('test1 = sin(col() + row())')
print gscript.read_command('r.info', map='test1', flags='g')
print gscript.parse_command('r.univar', map='test1', flags='g')
Best Answer
If you have already started GRASS interactively, and created a LOCATION/MAPSET, then try the full code example:from the wiki.
If you do not yet have your GRASS DB setup, and you want to try constructing a temporary location using python, then the next wiki section gives a working example.
Please try those complete examples, then post back if something does not work.
Edited:
You should not have to do any
pip install grass
. In the setup script are the lines:If you have GRASS installed, that should take care of finding the GRASS python module
grass.script
. Can you double check that you are getting the paths correct, etc.??