[GRASS-dev] [GRASS-user] How to embed a grass script (for example v.db.addcol) into a Pythonscript on windows?

Glynn,

A couple of students are trying to run python in wingrass now, following your directions below. They installed python, but we’re not sure where to put #4 in your steps.

  1. PYTHONPATH needs to be set to %WINGISBASE%\etc\python

It seems like it ought to go into the “grass64svn” initialization file

That file has the following lines…

PYTHONPATH=“$GISBASE/etc/python:$GISBASE/Python25:$PYTHONPATH”

export PYTHONPATH
PYTHONHOME=“C:\Program Files\GRASS-64-SVN\Python25”

Does it go there? With quotes? What about changing PYTHONHOME?

Is this the wrong place?

When we’ve changed the PYTHONPATH line in grass64svn and tried to run python from the msys prompt in GRASS, nothing happens. That is, the prompt goes away and returns a blank line. You can type on that line, but nothing returns.

What are we missing?

Thanks
Michael


C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 SHESC), 480-727-0709 (CSDC)

www: www.public.asu.edu/~cmbarton, http://csdc.asu.edu

Begin forwarded message:

From: Glynn Clements <glynn@gclements.plus.com>

Date: March 4, 2010 2:48:49 PM MST

To: Michael Barton <Michael.Barton@asu.edu>

Cc: grass-user grass-user <grass-user@lists.osgeo.org>, GRASS developers list <grass-dev@lists.osgeo.org>

Subject: Re: [GRASS-user] How to embed a grass script (for example v.db.addcol) into a Pythonscript on windows?

Michael Barton wrote:

This implies that it is now possible to create python scripts that run in GRASS under windows???

That would be great news. Is there anything special that is needed for this to work?

  1. The Python interpreter (python.exe) needs to be in the PATH
  2. Python needs to be associated with the .py extension
  3. PATHEXT needs to include .py if you want to be able to omit the extension
  4. PYTHONPATH needs to be set to %WINGISBASE%\etc\python

1-3 should be taken care of by the Python installer. 4 needs to be
done by the startup (currently, this doesn’t appear to be the case on
Windows).


Glynn Clements <glynn@gclements.plus.com>

Michael Barton wrote:

A couple of students are trying to run python in wingrass now, following
your directions below. They installed python, but we're not sure where
to put #4 in your steps.

>> 4. PYTHONPATH needs to be set to %WINGISBASE%\etc\python

It seems like it ought to go into the "grass64svn" initialization file

That file has the following lines...

PYTHONPATH="$GISBASE/etc/python:$GISBASE/Python25:$PYTHONPATH"
export PYTHONPATH
PYTHONHOME="C:\Program Files\GRASS-64-SVN\Python25"

Does it go there? With quotes? What about changing PYTHONHOME?

PYTHONPATH should be in Windows format. I don't know whether MSys will
convert it or not, but I wouldn't rely upon it.

PYTHONHOME shouldn't need to be set provided that Python was installed
correctly (i.e. using the .msi file from python.org). If you're doing
something else (e.g. trying to use a version of Python "bundled" with
GRASS), then I haven't got a clue.

FWIW, the following batch file works fine for me, with GRASS built
from source and installed with "make install" (i.e. no "installer" or
modifications to the startup files):

set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\GnuWin32\bin;C:\msys\1.0\local\bin;C:\Program Files\Python25
set GRASS_PYTHON=C:\Program Files\Python25\python.exe
set GRASS_WISH=wish84
set GRASS_SH=C:\msys\1.0\bin\sh.exe
"C:\Program Files\GRASS-6.5svn\grass65.bat" -text

Note: -text can be replaced with -tcltk or -wxpython; all work.

However, within GRASS, I also need:

set PYTHONPATH=%WINGISBASE%\etc\python

as init.bat only sets this if you start with -wxpython.

--
Glynn Clements <glynn@gclements.plus.com>

Thanks. Will try this.

Michael
____________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 SHESC), 480-727-0709 (CSDC)
www: www.public.asu.edu/~cmbarton, http://csdc.asu.edu

On Mar 18, 2010, at 4:03 AM, Glynn Clements wrote:

Michael Barton wrote:

A couple of students are trying to run python in wingrass now, following
your directions below. They installed python, but we're not sure where
to put #4 in your steps.

4. PYTHONPATH needs to be set to %WINGISBASE%\etc\python

It seems like it ought to go into the "grass64svn" initialization file

That file has the following lines...

PYTHONPATH="$GISBASE/etc/python:$GISBASE/Python25:$PYTHONPATH"
export PYTHONPATH
PYTHONHOME="C:\Program Files\GRASS-64-SVN\Python25"

Does it go there? With quotes? What about changing PYTHONHOME?

PYTHONPATH should be in Windows format. I don't know whether MSys will
convert it or not, but I wouldn't rely upon it.

PYTHONHOME shouldn't need to be set provided that Python was installed
correctly (i.e. using the .msi file from python.org). If you're doing
something else (e.g. trying to use a version of Python "bundled" with
GRASS), then I haven't got a clue.

FWIW, the following batch file works fine for me, with GRASS built
from source and installed with "make install" (i.e. no "installer" or
modifications to the startup files):

set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\GnuWin32\bin;C:\msys\1.0\local\bin;C:\Program Files\Python25
set GRASS_PYTHON=C:\Program Files\Python25\python.exe
set GRASS_WISH=wish84
set GRASS_SH=C:\msys\1.0\bin\sh.exe
"C:\Program Files\GRASS-6.5svn\grass65.bat" -text

Note: -text can be replaced with -tcltk or -wxpython; all work.

However, within GRASS, I also need:

set PYTHONPATH=%WINGISBASE%\etc\python

as init.bat only sets this if you start with -wxpython.

--
Glynn Clements <glynn@gclements.plus.com>

Hi Glynn,

We've tried the things below and are not getting anywhere. I don't know if we simply don't understand something (I'm not a Windows person, but the 2 students copied are) or there is something more we need to do.

So far, we've installed Python from python.org. We've installed it where it normally goes (at the C:\ root) and tried it in Programs.

We've substituted your batch file below for the one that comes with GRASS 6.4 svn for Windows.

After doing this and trying to run python at the MSys GRASS prompt, it simply comes back to a blank line, as if waiting for something. No Python shell. Is this the wrong way to test this?

If GRASS Python is working for you, we must be close. But I simply don't know how to proceed. Any advice would be welcome.

Michael
____________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 SHESC), 480-727-0709 (CSDC)
www: www.public.asu.edu/~cmbarton, http://csdc.asu.edu

On Mar 18, 2010, at 4:03 AM, Glynn Clements wrote:

Michael Barton wrote:

A couple of students are trying to run python in wingrass now, following
your directions below. They installed python, but we're not sure where
to put #4 in your steps.

4. PYTHONPATH needs to be set to %WINGISBASE%\etc\python

It seems like it ought to go into the "grass64svn" initialization file

That file has the following lines...

PYTHONPATH="$GISBASE/etc/python:$GISBASE/Python25:$PYTHONPATH"
export PYTHONPATH
PYTHONHOME="C:\Program Files\GRASS-64-SVN\Python25"

Does it go there? With quotes? What about changing PYTHONHOME?

PYTHONPATH should be in Windows format. I don't know whether MSys will
convert it or not, but I wouldn't rely upon it.

PYTHONHOME shouldn't need to be set provided that Python was installed
correctly (i.e. using the .msi file from python.org). If you're doing
something else (e.g. trying to use a version of Python "bundled" with
GRASS), then I haven't got a clue.

FWIW, the following batch file works fine for me, with GRASS built
from source and installed with "make install" (i.e. no "installer" or
modifications to the startup files):

set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\GnuWin32\bin;C:\msys\1.0\local\bin;C:\Program Files\Python25
set GRASS_PYTHON=C:\Program Files\Python25\python.exe
set GRASS_WISH=wish84
set GRASS_SH=C:\msys\1.0\bin\sh.exe
"C:\Program Files\GRASS-6.5svn\grass65.bat" -text

Note: -text can be replaced with -tcltk or -wxpython; all work.

However, within GRASS, I also need:

set PYTHONPATH=%WINGISBASE%\etc\python

as init.bat only sets this if you start with -wxpython.

--
Glynn Clements <glynn@gclements.plus.com>

Michael Barton wrote:

We've tried the things below and are not getting anywhere. I don't know
if we simply don't understand something (I'm not a Windows person, but
the 2 students copied are) or there is something more we need to do.

So far, we've installed Python from python.org. We've installed it where
it normally goes (at the C:\ root) and tried it in Programs.

We've substituted your batch file below for the one that comes with
GRASS 6.4 svn for Windows.

After doing this and trying to run python at the MSys GRASS prompt, it
simply comes back to a blank line, as if waiting for something. No
Python shell. Is this the wrong way to test this?

By "MSys GRASS prompt", are you talking about running MSys' /bin/sh in
a Windows console, or in MSys' (broken and deprecated) rxvt program?

Note that Windows versions of Python won't exhibit "interactive"
behaviour (prompt, input editing) within rxvt, because it isn't a tty
according to MSVCRT's isatty() function.

If you're using rxvt, you're on your own. Even using /bin/sh may be an
issue due to the conversions between Windows and MSys filename
formats.

The batch file which I posted results in a GRASS session using cmd.exe
within a Windows console.

If GRASS Python is working for you, we must be close. But I simply don't
know how to proceed. Any advice would be welcome.

I can only tell you what works for me, namely using native Windows
versions in preference to MSys, using GnuWin32 libraries in preference
to MSys, not using anything from OSGeo4W (except possibly GDAL), not
using the Windows "packaging" in the GRASS source tree, etc. Just
configure, make, make install, then a batch file to set a few
essential environment variables before starting grass65.bat.

--
Glynn Clements <glynn@gclements.plus.com>

OK. This helps. We were trying it in the "MSys' (broken and deprecated) rxvt program".

Like I said, I'm not familiar enough with Windows to realize that this would not work. I'm trying to help get python GRASS scripts to work under Windows. So testing it from MSys is not the way to go. Should we just try running a python script from the wxpython command prompt? Or should we do it from the MSys prompt?

Is there a shebang that we should be using on Windows that is different from the stock one (#!/usr/bin/env python).

I'm looking for a cookbook approach that I can tell to students and other who would like to use GRASS on Windows. I understand that there are difficulties with this, but am trying to find way to make scripting useable for people who cannot compile on Windows, but can modify batch and other scripts. So far, we've

1) installed Python from the python site
2) installed the GRASS binary (not the OSGeo4Win version, but the standalone native Windows one)
3) replaced the startup batch file with the commands you provided

Do we need to do something else? Do we need to start a script in a special way (e.g., by exec-ing python first? Do we need batch files to launch python scripts like we do for bash scripts?

It will be helpful to many to get this worked out, even in a hacked way at the moment.

In a related question, maybe more for Colin, does GRASS 7 work in Windows? If so, what is the setup to allow it to run the scripts in Python?

Thanks
Michael
______________________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
Tempe, AZ 85287-2402
USA

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671(SHESC), 480-727-0709 (CSDC)
www: http://csdc.asu.edu, http://shesc.asu.edu
    http://www.public.asu.edu/~cmbarton

On Apr 1, 2010, at 1:54 PM, Glynn Clements wrote:

Michael Barton wrote:

We've tried the things below and are not getting anywhere. I don't know
if we simply don't understand something (I'm not a Windows person, but
the 2 students copied are) or there is something more we need to do.

So far, we've installed Python from python.org. We've installed it where
it normally goes (at the C:\ root) and tried it in Programs.

We've substituted your batch file below for the one that comes with
GRASS 6.4 svn for Windows.

After doing this and trying to run python at the MSys GRASS prompt, it
simply comes back to a blank line, as if waiting for something. No
Python shell. Is this the wrong way to test this?

By "MSys GRASS prompt", are you talking about running MSys' /bin/sh in
a Windows console, or in MSys' (broken and deprecated) rxvt program?

Note that Windows versions of Python won't exhibit "interactive"
behaviour (prompt, input editing) within rxvt, because it isn't a tty
according to MSVCRT's isatty() function.

If you're using rxvt, you're on your own. Even using /bin/sh may be an
issue due to the conversions between Windows and MSys filename
formats.

The batch file which I posted results in a GRASS session using cmd.exe
within a Windows console.

If GRASS Python is working for you, we must be close. But I simply don't
know how to proceed. Any advice would be welcome.

I can only tell you what works for me, namely using native Windows
versions in preference to MSys, using GnuWin32 libraries in preference
to MSys, not using anything from OSGeo4W (except possibly GDAL), not
using the Windows "packaging" in the GRASS source tree, etc. Just
configure, make, make install, then a batch file to set a few
essential environment variables before starting grass65.bat.

--
Glynn Clements <glynn@gclements.plus.com>

Michael Barton wrote:

OK. This helps. We were trying it in the "MSys' (broken and deprecated)
rxvt program".

Like I said, I'm not familiar enough with Windows to realize that this
would not work. I'm trying to help get python GRASS scripts to work
under Windows. So testing it from MSys is not the way to go. Should we
just try running a python script from the wxpython command prompt? Or
should we do it from the MSys prompt?

First, I would suggest using the batch file which I posted (or
something quite similar) to start a session using cmd.exe in a Windows
console.

For running a *script*, it shouldn't matter whether you use rxvt or a
console, but it will matter for running Python interactively. Using
bash might cause problems, so it's best to avoid that at first.

Is there a shebang that we should be using on Windows that is different
from the stock one (#!/usr/bin/env python).

Shebangs don't work with Windows; that's a feature of MSys' bash.
Windows uses the extension.

If you've installed Python using the stock .msi installer from
python.org, it should have associated the .py extension with the
python.file type, and the python.file type with something like:

  "C:\Python26\python.exe" "%1" %*

I'm looking for a cookbook approach that I can tell to students and
other who would like to use GRASS on Windows. I understand that there
are difficulties with this, but am trying to find way to make scripting
useable for people who cannot compile on Windows, but can modify batch
and other scripts. So far, we've

1) installed Python from the python site
2) installed the GRASS binary (not the OSGeo4Win version, but the
standalone native Windows one)
3) replaced the startup batch file with the commands you provided

Do we need to do something else? Do we need to start a script in a
special way (e.g., by exec-ing python first? Do we need batch files to
launch python scripts like we do for bash scripts?

1. The Python scripts need to have a .py extension.
2. You may need:

  set PYTHONPATH=%WINGISBASE%\etc\python

as grass65.bat (etc) only sets this when invoked with -wxpython.

In a related question, maybe more for Colin, does GRASS 7 work in
Windows? If so, what is the setup to allow it to run the scripts in
Python?

GRASS 7 works in Windows using a similar batch file to that which I
posted for 6.x. However, the Init.bat file needs some work. Starting
with -wxpython doesn't work (you can start it manually via g.gui,
though). If you start in -text mode, you'll get a couple of errors
about the missing set_data program, but you'll end up in a session
using the existing .grassrc7 file (provided that one exists).

--
Glynn Clements <glynn@gclements.plus.com>

Hi Michael,

at the moment the WinGrass64-Installer ships it's own copy of python/wxpython (installed in C:\Program Files\GRASS-64-SVN\Python25).
In the starting scripts (grass64.bat, Init.bat) the path to this python-copy is set.

the wingrass-installer adds in the windows-start-menue "GRASS Command Line". this starts Grass64 in text mode in a normal Windows-console (not in rxvt), but you can start the wxgui from this Windows-console inside a grass-session by the command "g.gui wxpython".

and in this windows-console with the grass-session you have access to the wingrass-python-copy (installed in C:\Program Files\GRASS-64-SVN\Python25). for example you can start python inside a grass-session.

maybe this could be a starting point for testing python-scripting in wingrass64?

best regards
Helmut

---------------------
Von: Michael Barton <Michael.Barton@asu.edu>
Gesendet: 03.04.2010 01:14:31
An: "Colin (Nielsen) Wren" <colin.nielsen@gmail.com>,Helmut Kudrnovsky <hellik@web.de>
Betreff: Fwd: [GRASS-dev] Fwd: [GRASS-user] How to embed a grass script (for example v.db.addcol) into a Pythonscript on windows?

Colin and Helmut,

Is there some way that these suggestions can be implemented as default behavior in GRASS for Windows, or at least as an option? It is important that we can run scripts from Windows. Unfortunately, I don't understand this completely at the moment, but hope to test it out with students to get a clearer idea of how it all works.

Michael

______________________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
Tempe, AZ 85287-2402
USA

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671(SHESC), 480-727-0709 (CSDC)
www: [http://csdc.asu.edu], [http://shesc.asu.edu]
[http://www.public.asu.edu/~cmbarton\]

Begin forwarded message:

From: Glynn Clements <[glynn@gclements.plus.com]>

Date: April 2, 2010 3:54:22 PM MST

To: Michael Barton <[Michael.Barton@asu.edu]>

Cc: Isaac Ullah <[Isaac.Ullah@asu.edu]>, Andrea Torvinen <[atorvine@asu.edu]>, GRASS developers grass-developers <[grass-dev@lists.osgeo.org]>

Subject: Re: [GRASS-dev] Fwd: [GRASS-user] How to embed a grass script (for example v.db.addcol) into a Pythonscript on windows?

Michael Barton wrote:

OK. This helps. We were trying it in the "MSys' (broken and deprecated)

rxvt program".

Like I said, I'm not familiar enough with Windows to realize that this

would not work. I'm trying to help get python GRASS scripts to work

under Windows. So testing it from MSys is not the way to go. Should we

just try running a python script from the wxpython command prompt? Or

should we do it from the MSys prompt?

First, I would suggest using the batch file which I posted (or
something quite similar) to start a session using cmd.exe in a Windows
console.

For running a *script*, it shouldn't matter whether you use rxvt or a
console, but it will matter for running Python interactively. Using
bash might cause problems, so it's best to avoid that at first.

Is there a shebang that we should be using on Windows that is different

from the stock one (#!/usr/bin/env python).

Shebangs don't work with Windows; that's a feature of MSys' bash.
Windows uses the extension.

If you've installed Python using the stock .msi installer from
[python.org], it should have associated the .py extension with the
python.file type, and the python.file type with something like:

"C:\Python26\python.exe" "%1" %*

I'm looking for a cookbook approach that I can tell to students and

other who would like to use GRASS on Windows. I understand that there

are difficulties with this, but am trying to find way to make scripting

useable for people who cannot compile on Windows, but can modify batch

and other scripts. So far, we've

1) installed Python from the python site

2) installed the GRASS binary (not the OSGeo4Win version, but the

standalone native Windows one)

3) replaced the startup batch file with the commands you provided

Do we need to do something else? Do we need to start a script in a

special way (e.g., by exec-ing python first? Do we need batch files to

launch python scripts like we do for bash scripts?

1. The Python scripts need to have a .py extension.
2. You may need:

set PYTHONPATH=%WINGISBASE%\etc\python

as grass65.bat (etc) only sets this when invoked with -wxpython.

In a related question, maybe more for Colin, does GRASS 7 work in

Windows? If so, what is the setup to allow it to run the scripts in

Python?

GRASS 7 works in Windows using a similar batch file to that which I
posted for 6.x. However, the Init.bat file needs some work. Starting
with -wxpython doesn't work (you can start it manually via g.gui,
though). If you start in -text mode, you'll get a couple of errors
about the missing set_data program, but you'll end up in a session
using the existing .grassrc7 file (provided that one exists).

--
Glynn Clements <[glynn@gclements.plus.com]>
___________________________________________________________
NEU: WEB.DE DSL für 19,99 EUR/mtl. und ohne Mindest-Laufzeit!
http://produkte.web.de/go/02/

---------------------
Von: Michael Barton <Michael.Barton@asu.edu>
In a related question, maybe more for Colin, does GRASS 7 work in
Windows? If so, what is the setup to allow it to run the scripts in
Python?

GRASS 7 works in Windows using a similar batch file to that which I
posted for 6.x. However, the Init.bat file needs some work. Starting
with -wxpython doesn't work (you can start it manually via g.gui,
though). If you start in -text mode, you'll get a couple of errors
about the missing set_data program, but you'll end up in a session
using the existing .grassrc7 file (provided that one exists).

for testing there is a nightly build of WinGrass7:

http://grass.osgeo.org/download/software.php
http://josef.fsv.cvut.cz/wingrass/grass70/

best regards
Helmut
___________________________________________________________
NEU: WEB.DE DSL für 19,99 EUR/mtl. und ohne Mindest-Laufzeit!
http://produkte.web.de/go/02/

We tried starting GRASS in a dos window rather than msys using the command line option. This was a big improvement. We can run basic python scripts and start an interactive python session. So now we move on to new issues about running scripts.

1) scripts that call subprocess for some reason try to import threading which tries to import "collection". Collection does not exist. Very bizarre. Could this be a parser issue?

2) scripts that attempt to import the grass_script library cannot find it. Somehow this needs to get into the path/pythonpath at startup. $GISBASE/etc/python

Getting closer to python and GRASS in Windows.

Michael
____________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 SHESC), 480-727-0709 (CSDC)
www: www.public.asu.edu/~cmbarton, http://csdc.asu.edu

On Apr 3, 2010, at 9:17 AM, Helmut Kudrnovsky wrote:

Hi Michael,

at the moment the WinGrass64-Installer ships it's own copy of python/wxpython (installed in C:\Program Files\GRASS-64-SVN\Python25).
In the starting scripts (grass64.bat, Init.bat) the path to this python-copy is set.

the wingrass-installer adds in the windows-start-menue "GRASS Command Line". this starts Grass64 in text mode in a normal Windows-console (not in rxvt), but you can start the wxgui from this Windows-console inside a grass-session by the command "g.gui wxpython".

and in this windows-console with the grass-session you have access to the wingrass-python-copy (installed in C:\Program Files\GRASS-64-SVN\Python25). for example you can start python inside a grass-session.

maybe this could be a starting point for testing python-scripting in wingrass64?

best regards
Helmut

---------------------
Von: Michael Barton <Michael.Barton@asu.edu>
Gesendet: 03.04.2010 01:14:31
An: "Colin (Nielsen) Wren" <colin.nielsen@gmail.com>,Helmut Kudrnovsky <hellik@web.de>
Betreff: Fwd: [GRASS-dev] Fwd: [GRASS-user] How to embed a grass script (for example v.db.addcol) into a Pythonscript on windows?

Colin and Helmut,

Is there some way that these suggestions can be implemented as default behavior in GRASS for Windows, or at least as an option? It is important that we can run scripts from Windows. Unfortunately, I don't understand this completely at the moment, but hope to test it out with students to get a clearer idea of how it all works.

Michael

______________________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
Tempe, AZ 85287-2402
USA

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671(SHESC), 480-727-0709 (CSDC)
www: [http://csdc.asu.edu], [http://shesc.asu.edu]
[http://www.public.asu.edu/~cmbarton\]

Begin forwarded message:

From: Glynn Clements <[glynn@gclements.plus.com]>

Date: April 2, 2010 3:54:22 PM MST

To: Michael Barton <[Michael.Barton@asu.edu]>

Cc: Isaac Ullah <[Isaac.Ullah@asu.edu]>, Andrea Torvinen <[atorvine@asu.edu]>, GRASS developers grass-developers <[grass-dev@lists.osgeo.org]>

Subject: Re: [GRASS-dev] Fwd: [GRASS-user] How to embed a grass script (for example v.db.addcol) into a Pythonscript on windows?

Michael Barton wrote:

OK. This helps. We were trying it in the "MSys' (broken and deprecated)

rxvt program".

Like I said, I'm not familiar enough with Windows to realize that this

would not work. I'm trying to help get python GRASS scripts to work

under Windows. So testing it from MSys is not the way to go. Should we

just try running a python script from the wxpython command prompt? Or

should we do it from the MSys prompt?

First, I would suggest using the batch file which I posted (or
something quite similar) to start a session using cmd.exe in a Windows
console.

For running a *script*, it shouldn't matter whether you use rxvt or a
console, but it will matter for running Python interactively. Using
bash might cause problems, so it's best to avoid that at first.

Is there a shebang that we should be using on Windows that is different

from the stock one (#!/usr/bin/env python).

Shebangs don't work with Windows; that's a feature of MSys' bash.
Windows uses the extension.

If you've installed Python using the stock .msi installer from
[python.org], it should have associated the .py extension with the
python.file type, and the python.file type with something like:

"C:\Python26\python.exe" "%1" %*

I'm looking for a cookbook approach that I can tell to students and

other who would like to use GRASS on Windows. I understand that there

are difficulties with this, but am trying to find way to make scripting

useable for people who cannot compile on Windows, but can modify batch

and other scripts. So far, we've

1) installed Python from the python site

2) installed the GRASS binary (not the OSGeo4Win version, but the

standalone native Windows one)

3) replaced the startup batch file with the commands you provided

Do we need to do something else? Do we need to start a script in a

special way (e.g., by exec-ing python first? Do we need batch files to

launch python scripts like we do for bash scripts?

1. The Python scripts need to have a .py extension.
2. You may need:

set PYTHONPATH=%WINGISBASE%\etc\python

as grass65.bat (etc) only sets this when invoked with -wxpython.

In a related question, maybe more for Colin, does GRASS 7 work in

Windows? If so, what is the setup to allow it to run the scripts in

Python?

GRASS 7 works in Windows using a similar batch file to that which I
posted for 6.x. However, the Init.bat file needs some work. Starting
with -wxpython doesn't work (you can start it manually via g.gui,
though). If you start in -text mode, you'll get a couple of errors
about the missing set_data program, but you'll end up in a session
using the existing .grassrc7 file (provided that one exists).

--
Glynn Clements <[glynn@gclements.plus.com]>
___________________________________________________________
NEU: WEB.DE DSL für 19,99 EUR/mtl. und ohne Mindest-Laufzeit!
http://produkte.web.de/go/02/

Michael Barton wrote:

We tried starting GRASS in a dos window rather than msys using the
command line option. This was a big improvement. We can run basic python
scripts and start an interactive python session. So now we move on to
new issues about running scripts.

1) scripts that call subprocess for some reason try to import threading
which tries to import "collection". Collection does not exist. Very
bizarre. Could this be a parser issue?

Either the Python installation is broken or the variables are
misconfigured. Pointing PYTHONHOME at a version other than the one
with which the .py extension is associated will do this.

In 2.5, the collections module is built into the interpreter; in 2.6,
it's a separate file (Lib\collections.py).

2) scripts that attempt to import the grass_script library cannot find
it. Somehow this needs to get into the path/pythonpath at startup.
$GISBASE/etc/python

This may have to be done manually. AFAICT, the 6.x init.bat file only
sets PYTHONPATH if you start GRASS with the -wxpython switch.

--
Glynn Clements <glynn@gclements.plus.com>