2

I am trying to learn how to do some scripting to run Grass externally with Python but I can't seem to even find the basic modules called for this type of script. All of the documentation I have seen describes using grass.scripts module but I don't know where to download this. I also saw some information on grass.pygrass but I can't find this either.

I thought that maybe it was builtin to a newer version of Grass so I just downloaded 7.0.0 beta2 and I still find grass.scripts files. Is this a python module like others (matploblib, numpy, scipy etc.) or is it internal to Grass? Sorry for the remedial questions but I am lost here.

I ran the following script (taken from http://grasswiki.osgeo.org/wiki/GRASS_and_Python with quotations added since I run Python 2.7)

GISBASE= 'C:\GRASS-64'
GISRC= 'C:\Documents and Settings\user\.grassrc6'
LD_LIBRARY_PATH= 'C:\GRASS-64\lib'
PATH= 'C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26;'
PYTHONLIB= 'C:\Python26'
PYTHONPATH= 'C:\GRASS-64\etc\python'
GRASS_SH= 'C:\GRASS-64\msys\bin\sh.exe'

and it is fine (though I don't know what it does) but when I add

from grass.pygrass.modules import Module

it returns

ImportError: No module named gras.pygrass.modules

Normally I would download and install the module and the problem would be fixed but I can't find it anywhere.

Casivio
  • 333
  • 7
  • 15
  • Either that's not your actual code or that's not your actual error message, and it would be nice to rule out the possibility of a simple typo before digging into this any further… – abarnert Jul 29 '14 at 18:40
  • Actual code is # GrassRunner.py GISBASE= 'C:\Program Files\GRASS-6.4.3' GISRC= 'C:\Documents and Settings\user\.grassrc6' LD_LIBRARY_PATH= 'C:\Program Files\GRASS-6.4.3\lib' PATH= 'C:\Program Files\GRASS-6.4.3\etc;C:\Program Files\GRASS-6.4.3\etc\python;C:\Program Files\GRASS-6.4.3\lib;C:\Program Files\GRASS-6.4.3\bin;C:\Program Files\GRASS-6.4.3\extralib;C:\Program Files\GRASS-6.4.3\msys\bin;C:\Python27;' PYTHONLIB= 'C:\Python27' PYTHONPATH= 'C:\Program Files\GRASS-6.4.3\etc\python' GRASS_SH= 'C:\Program Files\GRASS-6.4.3\msys\bin\sh.exe' from grass.pygrass.modules import Module – Casivio Jul 30 '14 at 14:22
  • Sorry I would ignore that previous comment, I ran into the end of the allowed comment and can't edit any longer. That is actual code, cut and pasted, run with Python from Notepad++. Full error message is "Traceback (most recent call last): File "G:\Drawer\Tutorials\Python\Grass\GrassRunner.py", line 11, in (module) from grass.pygrass.modules import Module ImportError: No module named grass.pygrass.modules" – Casivio Jul 30 '14 at 15:04
  • You can't usefully paste code into comments. Also, your question really needs to have all the information necessary to describe the problem, or no new participants will try to help and no one with the same problem will be able to find it. So edit your question. Or, if you have additional information that isn't really part of the question, you can post somewhere like http://pastebin.com and put a link in the comments. – abarnert Jul 30 '14 at 18:01
  • Anyway, as I explained in a comment on an answer, the code you've copied isn't Python code. It's code that you have to run at the command prompt before running Python (or a list of environment variables that you have to set in some other way). – abarnert Jul 30 '14 at 18:02

2 Answers2

0

I ran the following script (taken from http://grasswiki.osgeo.org/wiki/GRASS_and_Python with quotations added since I run Python 2.7)

GISBASE= 'C:\GRASS-64'
GISRC= 'C:\Documents and Settings\user\.grassrc6'
LD_LIBRARY_PATH= 'C:\GRASS-64\lib'
PATH= 'C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26;'
PYTHONLIB= 'C:\Python26'
PYTHONPATH= 'C:\GRASS-64\etc\python'

I don't know why you think Python 2.7 requires you to add quotes. It doesn't.

But it does require you to replace all those Python26 with Python27. And that could easily be your problem. You don't have anything at C:\Python26.

And if you installed Grass somewhere other than C:\GRASS-64 you obviously need to change that as well. (From elsewhere in the file, I get the feeling that 64 refers to version 6.4, not to being 64-bits or something else, and you've downloaded 7.0.0, so I suspect it got installed to something like C:\GRASS-70.)


Or it could be this:

ImportError: No module named gras.pygrass.modules

If you import gras.pygrass.modules instead of grass.pygrass.modules, that's obviously not going to work.


Also, are you actually running that script in the same cmd.exe window that you use to start Python? If not, it's not going to help.


Meanwhile:

All of the documentation I have seen describes using grass.scripts module but I don't know where to download this.

The documentation you linked to answers that question, although it may not be obvious to a novice (once you figure this out, you might want to suggest documentation improvements upstream):

The related files are at $GISBASE/etc/python/grass/script/*.py.

On Windows, of course, that's $GISBASE isn't literally right; it's actually %GISBASE%, which is something like C:\GRASS-64, but it's the same thing. The files are already on your system; there is nothing to download. That's the whole point of setting PYTHONPATH to C:\GRASS-64\etc\python.

Is this a python module like others (matploblib, numpy, scipy etc.) or is it internal to Grass?

Well, it's not exactly "internal to Grass", but it's distributed with Grass, rather than as a separate module.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • abarnert, Thanks for the help. If I don't add the quotation marks, I get "SyntaxError: invalid syntax" pointing out the colon in the path for GISBASE. I am using Notepad++ so maybe it has something to do with that and not Python. I replaced all of the paths and it doesn't seem to matter because this code really doesn't do anything except define these items. Is that correct? That was simply a typo when I typed in the error message. You can see the call is correct. I don't start Python per say, I run it from Notepad++ so I don't know how that relates to the cmd window. – Casivio Jul 30 '14 at 15:23
  • I now see the files but it still seems to have the problem of not finding them for this call. Should it maybe include a path when importing? – Casivio Jul 30 '14 at 15:26
  • @CaseyGierke: You can't include a path when importing; you only get to specify the module name, not the pathname, and Python automatically adds the `.py`/`.pyc`/`.pyd`/etc. extension and searches each path on `sys.path` for a file with that name. So the key is to get the right paths onto `sys.path`. That's what that `PYTHONPATH` environment variable is for: any paths in that variable get added to `sys.paths` at Python startup. – abarnert Jul 30 '14 at 17:10
  • @CaseyGierke: Meanwhile, I don't know what "run it from Notepad++" means, but if it's starting Python for you, you need to either find a way to make it set the environment variables for you (the same way the script would do from the command line), or set them globally (in the system preferences—it used to be in the Environment tab of the System pane of the Preferences window, but I don't know where it is in current Windows), or of course stop using Notepad++ this way and just run your Python scripts out of a command prompt. – abarnert Jul 30 '14 at 17:12
0

I'm still not sure I understand everything but I seem to be past this hurdle. I used the script from (https://gis.stackexchange.com/questions/89452/problem-with-python-script-to-control-grass-gis-from-outside-how-to-import-gra/90160#90160) and changed all the relevant path information to suite my installation and apparently, now the grass.script module is accessed. Here is my working script

import os
import sys

gisbase = os.environ['GISBASE'] = 'C:\program files\grass gis 6.4.3'  #GISBASE needs to point the root of the GRASS installation directory
gisrc = 'C:\grassdata'
gisdbase = 'C:\grassdata'
location = 'newLocation'
mapset = 'TC'
LD_LIBRARY_PATH = 'C:\program files\grass gis 6.4.3\lib'
PATH = 'C:\program files\grass gis 6.4.3\etc';'C:\program files\grass gis 6.4.3\etc\python';'C:\program files\grass gis 6.4.3\lib';'C:\program files\grass gis 6.4.3\bin';'C:\Python27';'C:\program files\grass gis 6.4.3\Python27';'C:\program files\grass gis 6.4.3\msys'
PYTHONLIB = 'C:\Python27'
PYTHONPATH = 'C:\program files\grass gis 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
Community
  • 1
  • 1
Casivio
  • 333
  • 7
  • 15
  • It looks like you were trying to use the batch script from the original article as Python code. That explains why you needed the quotes: `GISBASE=C:\GRASS-64` is a perfectly legal line of cmd.exe batch-script code, but it's not legal Python code. It also explains why it wasn't helping even with the quotes: just setting a global variable named `PYTHONPATH` or `LD_LIBRARY_PATH` in Python doesn't affect the Python interpreter any way; you need to set them in the environment before launching Python. – abarnert Jul 30 '14 at 17:14
  • @abarnert: I am beginning to understand the separation between what I an wishing to do by running some script in Python and the preliminary leg work of setting up the proper environment before running. So is that something that I could just open the Grass GUI to do? I have never run a script through the command line and don't know much about environment setting in the command prompt. From what I understand about this now is that I would only need the 'gisbase=...' and the 'sys.path...' calls to direct python to the proper directory to open grass.script. Is that correct? – Casivio Jul 31 '14 at 15:47
  • I don't know much about the Grass GUI. But if you want to start developing independent Python scripts (rather than scripts that run inside the Grass app), you're almost certainly going to either need to learn the basics of using the command prompt, or pick an IDE (PyCharm, PyDev, etc.) and learn how to use that instead. – abarnert Jul 31 '14 at 17:08