【问题标题】:errors with python script using GRASS GIS API使用 GRASS GIS API 的 python 脚本错误
【发布时间】:2016-06-27 20:29:46
【问题描述】:

我已经从官方网站独立安装了 GRASS, 我使用 python 2.7 32b 和 windows 10。 我尝试使用 GRASS GIS 7.0.4 中的 python api。但我有导入错误。

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

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

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 导入一些其他导入?

【问题讨论】:

    标签: python python-2.7 api grass


    【解决方案1】:

    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 当前表示开发版本。对即将发布的 7.2 版本使用 grass72,对当前版本 7.0 (7.0.4) 使用 grass70

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

    您似乎正在尝试在 GRASS 会话之外在 Python 中使用 GRASS GIS,即没有明确启动 GRASS GIS。首先请注意,只有在您真正需要它时才这样做是好的。您应该使用的默认解决方案是编写一个 Python 脚本,该脚本将在 GRASS 会话中运行(即您启动 GRASS GIS,然后运行该脚本)。您可以从菜单File &gt; 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 票证 #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"
    

    您还可以在 MS Windows 注册表中永久更改 PATH 变量(例如使用 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 变量和环境变量的当前值中的空格、反斜杠、目录分隔符和分隔符。

    【讨论】:

    • 非常感谢您@wenzeslaus,这是一个很好的帮助,现在我将测试您的提示。但我有疑问如何使用 ctypes 修复“ImportError:gras_gis.7.0.4 not found”?我有 windows 10 和 python 2.7,我有 ctype lib。因为我认为这是 windows 中的一个大问题
    • 您需要在路径上添加库,因此您可以按照我所说的注释:将 GRASS GIS 安装中的 lib 目录添加到 PATH 变量中。我添加了一个如何在 MS Windows 上完成此操作的示例。
    • 您需要对您的问题提出确切的步骤,否则没有机会知道发生了什么。此外,您应该重新评估是否真的需要从 GRASS GIS 外部运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 2017-05-02
    • 1970-01-01
    相关资源
    最近更新 更多