This bat file starts spyder for me and imports grass.script fine:
@echo off
rem set OSGEO4W_ROOT=c:\OSGEO4W64
rem set PATH=%OSGEO4W_ROOT%\bin;%PATH%
call c:\osgeo4w64\bin\o4w_env.bat
call %OSGEO4W_ROOT%\apps\grass\grass-6.4.3\etc\env.bat
set GDAL_DRIVER_PATH=%OSGEO4W_ROOT%\bin\gdalplugins\1.9
set PATH=%PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
set PATH=%PATH%;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib
set PATH=%PATH%;%OSGEO4W_ROOT%\apps\Python27\Scripts\
set GISBASE=C:\OSGeo4W64\apps\grass\grass-6.4.3
set GISDBASE=d:\documents\grassdata
set GISRC=C:\Users\sando_000\AppData\Roaming\GRASS6\grassrc6
set GRASS_SH=C:\OSGeo4W\apps\msys\bin\sh.exe
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python;
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\etc\python
set QGISPATH=%OSGEO4W_ROOT%\apps\qgis
start c:\OSGeo4W64\apps\Python27\Scripts\spyder.bat
I've got an OSGE4W 64bit installation w grass-6.4.3, python-2.7 and spyder installed from source package with:
python setup.py install
This script is a modified version from what used to start pyscripter with qgis:
https://mapoholic.wordpress.com/2012/06/28/configure-pyscripter-qgis/
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
You cannot use a shapefile directly. You must first import it as a Grass layer in your mapset "Martin". A name beginning with a number (1.shp) is not allowed in Grass Also, in order to use GRASS functionality via Python from outside, some environment variables have to be set (see GRASS and Python):
after that you can use grass.script in the Python Shell:
see GRASS Python Scripting Library or Python Scripts For GRASS GIS