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.

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:Is it possible to use GRASS GIS in Python stand alone scripts?,GRASS and Python, in order to control GRASS from outside by using python.

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? Please give me any suggestion, thank you very much!


Update#1:

@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?


Update#2:(Thanks Martin)

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 = 
C:\Python27
C:\OSGeo4W64\apps\grass\grass-6.4.3
C:\Users\Heinz\Documents\grassdata
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\msys
C:\OSGeo4W64\apps\grass\grass-6.4.3\scripts
C:\OSGeo4W64\bin
C:\OSGeo4W64\apps\Python27\Lib\site-packages

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.


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

C:\OSGeo4W64\apps\Python27
C:\Python27
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.

ps. I also found this python-related file:

C:\Users\Heinz\Downloads\http%3a%2f%2fdownload.osgeo.org%2fosgeo4w%2f\x86_64\release\python

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 feel grateful that I have got these helps so far, and I will keep trying to make my script work!

share|improve this question
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 at 8:43
    
@Martin thank you, and I have edited my post! –  Heinz Mar 19 at 14:53
    
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 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 at 15:35
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 at 9:41
show 4 more comments

3 Answers 3

up vote 2 down vote accepted

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

share|improve this answer
    
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 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 at 7:06
1  
I´m glad you were able to solve your problem!! Congratulations –  Martin Mar 25 at 6:32
    
thanks a lot!!!! –  Heinz Mar 25 at 6:33
add comment

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.

share|improve this answer
    
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 at 14:18
1  
The path for GISRC= C:\Documents and Settings\user\.grassrc6is missing. Make sure that the file exists. –  ustroetz Mar 12 at 14:40
    
I have edited my post and create a file named .grassrc6 –  Heinz Mar 13 at 9:03
add comment
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
share|improve this answer
    
thanks! I edited my code as yours above and ran it, and I got the same error. –  Heinz Mar 12 at 14:56
    
sorry to hear that, I was assuming that your environment variable were correct –  Below the Radar Mar 12 at 15:07
    
did I miss something or make something wrong in my code? –  Heinz Mar 12 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 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 at 6:24
show 1 more comment

Your Answer

 
discard

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.