Saturday, 21 May 2016

Using Spyder IDE to develop python for Grass GIS, get 'ImportError: No module named grass.script'


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?




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...