[GIS] Importing grass.script as grass under windows 7

grassimportqgiswindows

OS: Windows 7 64bit

GIS: QGIS 1.8 (qgis.org/downloads/QGIS-OSGeo4W-1.8.0-2-Setup.exe)

I have created a plugin for QGIS, with the QGIS plugin builder.
Now I would like to import grass.script into this script

# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

# Initialize Qt resources from file resources.py
import resources_rc

# Import the code for the dialog
from testdialog import testDialog

# Import grass.script
import os, sys
import grass.script as grass

If I run this Script then I get the following error:
ImportError: No module named grass.script

The whole Error massage is:
Couldn't load plugin test due an error when calling its classFactory() method

Traceback (most recent call last):
  File "C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py", line 164, in startPlugin
    plugins[packageName] = package.classFactory(iface)
  File "C:/Users/admin/.qgis//python/plugins\test\__init__.py", line 52, in classFactory
    from test import test
  File "C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py", line 309, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:/Users/admin/.qgis//python/plugins\test\test.py", line 31, in 
    import grass.script as grass
  File "C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py", line 309, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: No module named grass.script

Python version:
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]


QGIS version:
1.8.0-Lisboa Lisboa, f440b60

Python path: ['C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python', 'C:/Users/admin/.qgis//python', 'C:/Users/admin/.qgis//python/plugins', 'C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\bin\\python27.zip', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\DLLs', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\plat-win', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\lib-tk', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\qgis\\bin', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\PIL', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\win32', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']

How could I fix this problem?

How could I "import grass.script as grass" into my script?


Now, i have add the following path variable:

GISBASE=C:\Program Files (x86)\Quantum GIS Lisboa\apps\grass\grass-6.4.3RC2
LD_LIBRARY_PATH=C:\Program Files (x86)\Quantum GIS Lisboa\apps\grass\grass-6.4.3RC2\lib

PATH=C:\Program Files (x86)\Quantum GIS Lisboa\apps\grass\grass-6.4.3RC2\etc;
C:\Program Files (x86)\Quantum GIS Lisboa\apps\grass\grass-6.4.3RC2\etc\python;
C:\Program Files (x86)\Quantum GIS Lisboa\apps\grass\grass-6.4.3RC2\bin;
C:\Program Files (x86)\Quantum GIS Lisboa\apps\msys\bin;
C:\Program Files (x86)\Quantum GIS Lisboa\apps\Python27;

PYTHONLIB=C:\Program Files (x86)\Quantum GIS Lisboa\apps\Python27
PYTHONPATH=C:\Program Files (x86)\Quantum GIS Lisboa\apps\grass\grass-6.4.3RC2\etc\python
GRASS_SH=C:\Program Files (x86)\Quantum GIS Lisboa\apps\msys\bin\sh.exe

Now I have another error but I will create a new question for this.

Best Answer

Sounds like a problem with your path settings. Did you consult this website beforehand? Try to set the path variable correct for QGIS-python (remember that QGIS ships with its own python setup and therefore has a separate path).
The OSGEO4W-Installer for Windows should allow to directly install the python-grass bindings within the QGIS python path (Select advanced Install)

Related Question