Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 ?

regards Marcus

--------------------- update ---------------------

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 an other error. But i create a new question for this.

Thx for helping

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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)

share|improve this answer
i add the path variables and i get an other error. WindowsError: [Error 6] The handle is invalid –  user16820 Apr 4 '13 at 12:25
i have updated my question –  user16820 Apr 4 '13 at 12:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.