1

我有来自官方网站的独立安装 GRASS,并且我使用 python 2.7 32b 和 windows 10。我尝试使用来自 GRASS GIS 7.0.4 的 python api。但我有导入错误。

一些导入工作我可以在 python ide 中导入,而一些导入不起作用,我无法使用。

首先我读了这篇文章grass python,我尝试使用pygrass api python代码:

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"))

成功导入

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

没有成功导入

  1. from pygrass.modules import Module

错误信息:No module named pygrass.modules

2.from grass.pygrass.gis import Mapset

错误信息:

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

错误信息 :

    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

错误信息 :

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

错误信息 :

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

错误信息 :

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

错误信息:

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

错误信息:ImportError: cannot import name raster

9.from grass.pygrass.functions import coor2pixel

错误信息:No module named functions

10.import grass.scripts

错误信息 :No module named scripts

知道为什么我可以从 pygrass 导入一些导入,为什么我不能从 pygrass 导入一些其他导入?

4

1 回答 1

2

import grass.scripts一个错字。您需要使用import grass.script,因为这是模块的名称。

导入from pygrass.modules import Module原因No module named pygrass.modules是因为没有这样的模块。该模块的名称grass.pygrass.modules与您在其他示例中使用的名称相同。

导入from grass.pygrass.modules import raster as r看起来像你想说的from grass.pygrass.modules.shortcuts import raster as r。您是从某些文档中使用的吗?

进口from grass.pygrass.functions import coor2pixel应该是. from grass.pygrass.utils import coor2pixel看起来您确实使用了一些过时的(错误的?)文档。最新的文档在这里:

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

grass73当前表示开发版本。用于grass72即将发布的 7.2 版本和grass70当前版本 7.0 (7.0.4)。

所有其他错误都表明ImportError: grass_gis.7.0.4 not found与前几行有关的错误(例如ctypes_loader.py...load_library)表明问题在于使用未找到的 C 类型加载 C 动态库。

看来您正试图在 GRASS 会话之外在 Python 中使用 GRASS GIS,即没有明确启动 GRASS GIS。首先请注意,只有在您真正需要它时才这样做是好的。您应该使用的默认解决方案是编写一个将在 GRASS 会话中运行的 Python 脚本(即您启动 GRASS GIS,然后运行该脚本)。你从菜单做File > Run script或从命令行。开发版本甚至包含一个简单的 Python 编辑器,它具有一些特定于 GRASS GIS 的特殊功能。编写在 GRASS 会话中运行的脚本无需在脚本中设置环境,您只需专注于功能。它还将使其更容易在不同的计算机上使用,而无需更改脚本中的路径。最后,它允许您轻松使用自动生成的 GUI 之类的东西。如果您想在不使用 GRASS GIS GUI 的情况下自动/以编程方式启动脚本,您可以查看grass( grass7, grass70, ...) 命令帮助提供的内容以及为脚本设置GRASS_BATCH_JOB变量的选项。开发版本还具有--exec更多功能的选项:

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

如果您确实需要自己设置 GRASS GIS 运行时环境,请确保您使用的是以下文档:

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

但是,PyGRASS 依赖于通过 ctypes 使用并在导入期间加载的动态库。操作系统使用其设置和当前环境变量搜索库。对于动态库,它使用进程启动时的环境变量。因此,稍后在脚本中更改它,例如,grass.script.setup.init()这样做不会影响流程本身,而只会影响其子流程。因此,您需要为 Linux 执行以下命令:

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

在 GRASS GIS ticket #2424 中查看更深入的讨论:

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

在 MS Windows 上,库的路径存储在PATH变量中(与可执行文件的路径混合),因此您可以在PATH那里使用。你需要做这样的事情:

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

您还可以PATH在 MS Windows 注册表中永久更改变量(例如,使用xset)。网上有很多关于这方面的资料(它对任何程序都一样)。

此外,在处理 Python 中的路径时,我建议使用,os.path.join因为在处理 MS Windows 上的文件路径时有一些细节。有关详细信息,请参阅 Python 文档:

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

这同样适用于构造PATH变量和其他包含路径的变量。通常(在命令行或 Python 中),请注意PATH/变量中的空格、反斜杠、目录分隔符和分隔符LD_LIBRARY_PATH以及环境变量的当前值。

于 2016-06-28T17:50:16.790 回答