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:
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:
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:
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:\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.
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:
I will keep trying to make my script work!
Answer
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:
- Go to the folder
C:\OSGeo4W\apps\python27\lib\site-packages
- Create a file called
grass.pth
and open it with an editor 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
Save and close the file
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
No comments:
Post a Comment