1

i have standalone install GRASS from official web site, and i using python 2.7 32b and windows 10. i trying to works with python api from GRASS GIS 7.0.4. but i have import errors.

some imports work i can import in the python ide and some imports not works and i cant to using.

first i read this post grass python and i trying to take pygrass api python code :

import os, sys
import subprocess as subp
gisbase = os.environ['GISBASE'] = "C:\Program Files (x86)\GRASS GIS 7.0.4"
gisdbase = os.path.join(os.environ['HOME'])
location = "test"
mapset   = "PERMANENT"
 sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python","bin","extrabin","lib","driver","scripts"))

success imports

import grass.script as grass
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.modules import Module as run_command
from grass.pygrass.modules.shortcuts import general as g
from grass import script as g
from grass.script import setup as gsetup
from grass.pygrass.modules.shortcuts import imagery as i
from pygrass.modules import Module

no success imports

  1. from pygrass.modules import Module

error message: No module named pygrass.modules

2.from grass.pygrass.gis import Mapset

error message:

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    from grass.pygrass.gis import Mapset
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\pygrass\gis\__init__.py", line 13, in <module>
    import grass.lib.gis as libgis
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\gis.py", line 23, in <module>
    _libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\ctypes_loader.py", line 57, in load_library
    raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.

3.from grass.pygrass.vector import VectorTopo

error message :

    Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    from grass.pygrass.vector import VectorTopo
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\pygrass\vector\__init__.py", line 3, in <module>
    import grass.lib.gis as libgis
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\gis.py", line 23, in <module>
    _libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\ctypes_loader.py", line 57, in load_library
    raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.

4.from grass.pygrass.vector import Vector

error message :

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    from grass.pygrass.vector import Vector
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\pygrass\vector\__init__.py", line 3, in <module>
    import grass.lib.gis as libgis
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\gis.py", line 23, in <module>
    _libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\ctypes_loader.py", line 57, in load_library
    raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.

5.from grass.pygrass.raster import RasterRow

error message :

Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    from grass.pygrass.raster import RasterRow
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\pygrass\raster\__init__.py", line 14, in <module>
    import grass.lib.gis as libgis
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\gis.py", line 23, in <module>
    _libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\ctypes_loader.py", line 57, in load_library
    raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.

6.from grass.pygrass.gis import Location

error message :

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    from grass.pygrass.gis import Location
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\pygrass\gis\__init__.py", line 13, in <module>
    import grass.lib.gis as libgis
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\gis.py", line 23, in <module>
    _libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\ctypes_loader.py", line 57, in load_library
    raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.

7.from grass.pygrass.gis.region import Region

error message:

Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    from grass.pygrass.gis.region import Region
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\pygrass\gis\__init__.py", line 13, in <module>
    import grass.lib.gis as libgis
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\gis.py", line 23, in <module>
    _libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
  File "C:\Program Files (x86)\GRASS GIS 7.0.4\etc\python\grass\lib\ctypes_loader.py", line 57, in load_library
    raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.

8.from grass.pygrass.modules import raster as r

error message: ImportError: cannot import name raster

9.from grass.pygrass.functions import coor2pixel

error message: No module named functions

10.import grass.scripts

error message :No module named scripts

any idea why to i can import some imports from the pygrass and why i cant import some other imports from pygrass ?

Community
  • 1
  • 1
Chris Papas
  • 249
  • 5
  • 20

1 Answers1

2

The import grass.scripts is a typo. You need to use import grass.script because that's the name of the module.

The import from pygrass.modules import Module causes No module named pygrass.modules because there is no such module. The name for the module is grass.pygrass.modules as you are using it in the other examples.

The import from grass.pygrass.modules import raster as r looks like you want to say from grass.pygrass.modules.shortcuts import raster as r. Is what you are using from some documentation?

The from grass.pygrass.functions import coor2pixel import should be from grass.pygrass.utils import coor2pixel. It really looks like you are using some outdated (wrong?) documentation. The latest documentation is here:

https://grass.osgeo.org/grass73/manuals/libpython/pygrass_index.html

grass73 currently denotes the development version. Use grass72 for upcoming 7.2 release and grass70 for the current release 7.0 (7.0.4).

All the other errors say ImportError: grass_gis.7.0.4 not found which in connection with the previous lines (e.g. ctypes_loader.py...load_library) shows that the problem is in loading the C dynamic libraries using C types which are not found.

It seems that you are trying to use GRASS GIS in Python from outside of GRASS session, i.e. without starting GRASS GIS explicitly. First note, that this is good to do only when you actually need it. The default solution you should use is to write a Python script which will run inside GRASS session (i.e. you start GRASS GIS and then you run the script). You do it from menu File > Run script or from the command line. The development version even contains an simple Python editor which has some special functions specific to GRASS GIS. Writing a script which runs inside a GRASS session removes from you the need to setup the environment in the script and you can just focus on the functionality. It will also make it easier to use it on different computers without changing the paths in the script. Finally, it allows you to use easily things like the automatically generated GUI. In case you want to start the script automatically/programmatically without using GRASS GIS GUI, you can look at what the grass (grass7, grass70, ...) command help gives and option to set GRASS_BATCH_JOB variable to your script. The development version has also --exec option which is little bit more versatile:

https://grass.osgeo.org/grass72/manuals/grass7.html

If you really need to want setup the GRASS GIS runtime environment yourself, make sure you are using the following the documentation:

https://grass.osgeo.org/grass73/manuals/libpython/script.html#module-script.setup

However, PyGRASS depends on the dynamic libraries which are used through ctypes and loaded during import. The operating system searches for the libraries using its settings and current environmental variables. For the dynamic libraries, it uses the environmental variables from the time the process was started. So changing it later in the script, for example as grass.script.setup.init() does, does not influence the process itself but only its subprocesses. Consequently, you need to do something like the following commands for Linux:

export LD_LIBRARY_PATH=$(grass70 --config path)/lib
python a_python_script.py

See a more in-depth discussion in GRASS GIS ticket #2424:

https://trac.osgeo.org/grass/ticket/2424

On MS Windows, the paths to libraries are stored in the PATH variable (mixed with paths to executables), so you to use PATH there. You need to do something like this:

set PATH="%PATH%;C:\Program Files (x86)\GRASS GIS 7.0.4\lib"

You can also change the PATH variable permanently in the MS Windows registry (e.g. with xset). There is plenty of material online about that (it works the same for any program).

Also, when dealing with paths in Python, I recommend using os.path.join because there are some specifics in dealing with file paths on MS Windows. See the Python documentation for details:

https://docs.python.org/2/library/os.path.html#os.path.join

The same applies to constructing PATH variable and other variables containing paths. In general (in command line or Python), be aware of spaces, backslashes, directory separators and separators in PATH/LD_LIBRARY_PATH variables and current values of environmental variables.

wenzeslaus
  • 667
  • 6
  • 13
  • thnx you very much @wenzeslaus this is a good help now i will test your tips.but i have question how to fix 'ImportError: grass_gis.7.0.4 not found' with ctypes? i have windows 10 and python 2.7 and i have ctype lib.because i think this is a big proplem in windows – Chris Papas Jun 29 '16 at 07:10
  • You need to add the libraries on path, so you can do what I said the comment: add lib directory from the GRASS GIS installation to the PATH variable. I added an example how this can be done on MS Windows. – wenzeslaus Jun 29 '16 at 16:31
  • You will need to put exact steps to your question, otherwise there is no chance to know what is happening. Also, you should re-evaluate if you really need to run the thing from outside GRASS GIS. – wenzeslaus Jun 29 '16 at 20:21