[GIS] Using Spyder IDE to develop python for Grass GIS, get ‘ImportError: No module named grass.script’

grassgrass.scriptimporterrorpythonspyder

I am trying to use Spyder IDE to develop a python script using python library from grass-6.4.3. I run the script below, and get

ImportError: No module named grass.script

I am using python 2.7, installed in Grass, Spyder, + native install (C:\python27) and my OS is 64-bit Windows 7.

This seems to be a common problem, but I have tried all the recommended fixes outlined below. I have even tried installing on a laptop running 32-bit windows 7 and get the same error.

I read the Grass wiki: page:https://grasswiki.osgeo.org/wiki/GRASS_and_Python#External_Python_editors_.28IDE.29

Then I tried to follow this Q&A to the letter: Problem with python script to control GRASS GIS from outside – How to import grass.script under Win 8.1?

My python script:

import sys
import os

gisbase = os.environ['GISBASE'] = 'C:\OSGeo4W64\apps\grass\grass-6.4.3'  
gisdbase = os.path.normpath('C:/Users/Ryan/grassdata')
location = 'newLocation'
mapset = 'Ryan'
GISRC = os.path.normpath('C:/Users/Ryan/AppData/Roaming/GRASS6/grassrc6')
LD_LIBRARY_PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib'
PATH = 'C:\Python27';'C:\Users\Ryan\grassdata';'C:\Users\Ryan\AppData\Roaming\GRASS6\grassrc6';'C:\OSGeo4W64\bin';'C:\OSGeo4W64\apps\grass\grass-6.4.3';'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\Python27\Lib\site-packages';'C:\OSGeo4W64\apps\msys';'C:\OSGeo4W64\apps\grass\grass-6.4.3\scripts'
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'))
sys.path.append('C:\OSGeo4W\bin')
sys.path.append('C:\OSGeo4W64\apps\grass\grass-6.4.3\etc') 
sys.path.append('C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python') 
sys.path.append('C:\OSGeo4W64\apps\grass\grass-6.4.3\lib') 
sys.path.append('C:\OSGeo4W64\apps\grass\grass-6.4.3\bin') 

import grass.script as grass
import grass.script.setup as gsetup

gsetup.init(gisbase, gisdb, location, mapset)

I have added my environment variables manually into system settings (copied here):

GISBASE = C:\OSGeo4W64\apps\grass\grass-6.4.3
GISDBASE = C:\Users\Ryan\grassdata
GISRC = C:\Users\Ryan\AppData\Roaming\GRASS6\grassrc6
GRASS_SH = C:\OSGeo4W\apps\msys\bin\sh.exe
LD_LIBRARY_PATH = C:\OSGeo4W64\apps\grass\grass-6.4.3\lib
PATH = C:\Python27;C:\Users\Ryan\grassdata;C:\Users\Ryan\AppData\Roaming\GRASS6\grassrc6;C:\OSGeo4W64\bin;C:\OSGeo4W64\apps\grass\grass-6.4.3;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\Python27\Lib\site-packages;C:\OSGeo4W64\apps\msys;C:\OSGeo4W64\apps\grass\grass-6.4.3\scripts
PYTHONLIB = C:\Python27
PYTHONPATH = C:\OSGeo4W\apps\grass\grass-6.4.3\etc\python'

I have added a file called 'grass.pth' to: C:\OSGeo4W\apps\python27\lib\site-packages. The file contains:

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

I have started grass interactively to create a 'grassrc6' file, which is located here: C:\Users\Ryan\AppData\Roaming\GRASS6\grassrc6

I am not sure what else I can try, I am certain something must be wrong with the environment variables, but I wouldn't know what to look for.

Also, is it necessary to define the environment variables in both the python script, and manually in system settings?

Best Answer

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/

Related Question