I am working with GRASS 6.4.3 and python 2.7.6 under WIN 8.1.

I am a novice with computer science and coding, and I have seen many posts including those:

Here's my python code, which wants to use the GRASS module g.list to list my raster data in GRASS. I type those code in Python IDLE.

import os
import sys

gisbase = os.environ['GISBASE'] = 'C:\OSGeo4W64\apps\grass\grass-6.4.3'  #GISBASE needs to point the root of the GRASS installation directory
gisrc = 'C:\Users\Heinz\Documents\grassdata'
gisdbase = 'C:\Users\Heinz\Documents\grassdata'
location = 'newLocation'
mapset = 'TC'
LD_LIBRARY_PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib'
PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc';'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python';'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib';'C:\OSGeo4W64\apps\grass\grass-6.4.3\bin';'C:\Python27';'C:\OSGeo4W64\apps\Python27';'C:\OSGeo4W64\apps\msys'
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'))

import grass.script as grass

And I have set every path in my code as environment variable in the windows control panel.

As I run module, I got the error:

enter image description here

enter image description here

Which line of my code is wrong or not necessary and what code should I add to my python script, or can't I use Python IDLE to do this?

@ustroetz I have found no file in my computer named .grassrc6 , so I create a new file named .grassrc6(which is empty), adding a line in my code:

gisrc = 'C:\Users\Heinz\.grassrc6'

But still got the same error.

Did I misunderstand the instruction in the post?

I create a text file named grass.pth in this folder C:\OSGeo4W\apps\python27\lib\site-packages, and type those two lines in it: enter image description here

Here is the list of my environment variables I have set in windows penal:

GISBASE = C:\OSGeo4W64\apps\grass\grass-6.4.3
GISRC = C:\Users\Heinz\Documents\grassdata
Path = 

then I ran my python script, and got the same error: enter image description here

You can see that I have added this line:sys.path.append('C:\OSGeo4W\bin') in my script(it's because the script didn't work before I added it), but it still got the same error.

@Martin, I have found that I have three places where python installed:

C:\Program Files (x86)\Quantum GIS Lisboa\apps\Python27

and I also found site-packages files in those places, so I copied grass.pth to those places, but my script still can't work.

I also found this python-related file:


I don't know if this keeps my script useless, should I delete this file, or it doesn't matter?

I have tried this in cmd: enter image description here

I will keep trying to make my script work!

  • 1
    Could you verify, that you have only one Python installation. It might be possible that you have a second installation (e.g. from ArcGIS). If you have two Python installations it might be necessary to link both together. Maybe IDLE is using the second Python installation. If this is the case copy the grass.pth to the sitepackages folder of the second Python installation – Martin Mar 19 '14 at 8:43
  • Now it would be interesting to know, which python installation IDLE is linked to. To test, if the .pth work, could you please open a commandline (cmd) in "C:\OSGeo4W\bin". Enter "python" and hit return. Then enter "import grass.script as grass". Do you get an error message? Try the same in "C:\python27" or where the python.exe is. – Martin Mar 19 '14 at 15:23
  • Just realizing that you have the 64-Bit version of OSGeo4W installed. That might be a reason for errors as well. Might be worth trying the 32-Bit if nothing else works – Martin Mar 19 '14 at 15:35
  • @Martin I have edited my post and showed one of the result in cmd, and I got the same error as I tried different directory. And I have verified that I installed 64-bit version of OSGeo4W, so should I uninstall it and reinstall the 32-bit version(my computer is WIN8.1 64-bit) – Heinz Mar 19 '14 at 15:56
  • 1
    Did you get the same result from cmd when going to the OSGeo4W python installation? (C:\OSGeo4W64\bin)? That the 64-Bit installation could cause problems is just a guess. There should be no problem installing the 32-Bit version of OSGeo4W on your 64-Bit machine. – Martin Mar 20 '14 at 9:41

After some hard work, I got my GRASS working with Python using a .pth file in the sitepackages folder.

To try this, do to the following steps:

  1. Go to the folder C:\OSGeo4W\apps\python27\lib\site-packages
  2. Create a file called grass.pth and open it with an editor
  3. Enter the following two lines (assuming your GRASS was installed with OSGeo4W; check the paths to be sure):

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

  4. Save and close the file

  5. Try the following statement in your Python Script

    import grass.script as grass

P.S.: To make that work, you have to set your enviroment variables as described in posts above.

Another option might be to add the C:\OSGeo4W\bin path to your script with sys.path.append as you did with other paths

Once it works, check out the following post for getting to run the grass tools: GRASS Geoprocessing in Python Script

  • thank you for your reply. I can't link python and GRASS though, and I will edit my post to let you know the detail. – Heinz Mar 19 '14 at 8:01
  • after I installed the 32-bit version, I took this post as reference,lists.osgeo.org/pipermail/grass-user/2011-July/061305.html, and re-arranged my script to put 'sys.path.append...' right under 'import sys', and it worked! Thank you, and now I am going to learn the grammar of python to control GRASS. – Heinz Mar 22 '14 at 7:06
  • 1
    I´m glad you were able to solve your problem!! Congratulations – Martin Mar 25 '14 at 6:32

You have not connected Grass GIS and Python probably. So there is nothing particular wrong with your code, but Python cannot import the grass.script module.

Probably you didn't set the environment variables yet. You can find good documentation on how to setup Grass GIS in order to use it in Python on the OSGEO Grass Wiki Site.

  • thanks for your reply! I have edited my post! I added three lines about path variables, but still got the same error. – Heinz Mar 12 '14 at 14:18
  • 1
    The path for GISRC= C:\Documents and Settings\user\.grassrc6is missing. Make sure that the file exists. – ustroetz Mar 12 '14 at 14:40
  • I have edited my post and create a file named .grassrc6 – Heinz Mar 13 '14 at 9:03
import sys
import os

gisbase = os.environ['GISBASE'] = 'C:\OSGeo4W64\apps\grass\grass-6.4.3'  #GISBASE needs to point the root of the GRASS installation directory
gisdbase = 'C:\Users\Heinz\Documents\grassdata'
LD_LIBRARY_PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib'
PATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc';'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python';'C:\OSGeo4W64\apps\grass\grass-6.4.3\lib';'C:\OSGeo4W64\apps\grass\grass-6.4.3\bin';'C:\Python27'
PYTHONLIB = 'C:\Python27'
PYTHONPATH = 'C:\OSGeo4W64\apps\grass\grass-6.4.3\etc\python'

sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))

import grass.scripts
  • thanks! I edited my code as yours above and ran it, and I got the same error. – Heinz Mar 12 '14 at 14:56
  • sorry to hear that, I was assuming that your environment variable were correct – Below the Radar Mar 12 '14 at 15:07
  • did I miss something or make something wrong in my code? – Heinz Mar 12 '14 at 15:09
  • not necessarly, your python cant find grass module. You have to set your environment variables correctly to tell python where to look for grass – Below the Radar Mar 12 '14 at 15:12
  • should I set environment variables in the windows control panel? Or just type in those environment variables in my python script? – Heinz Mar 13 '14 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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