python - 使用 GRASS GIS API 的 python 脚本错误

标签 python python-2.7 api grass

我从官方网站上独立安装了 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
没有成功导入
  • from pygrass.modules import Module

  • 错误信息:No module named pygrass.modules2. 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 raster9. from grass.pygrass.functions import coor2pixel错误信息:No module named functions10. import grass.scripts错误信息:No module named scripts知道为什么我可以从 pygrass 导入一些导入,为什么我不能从 pygrass 导入一些其他导入?

    最佳答案

    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 session 之外在 Python 中使用 GRASS GIS,即没有明确启动 GRASS GIS。首先请注意,只有在您真正需要它时才这样做是好的。您应该使用的默认解决方案是编写一个 Python 脚本,该脚本将在 GRASS session 中运行(即您启动 GRASS GIS,然后运行该脚本)。你可以从菜单 File > Run script或从命令行。开发版本甚至包含一个简单的 Python 编辑器,它具有一些特定于 GRASS GIS 的特殊功能。编写在 GRASS session 中运行的脚本无需在脚本中设置环境,您只需专注于功能。它还将使其更容易在不同的计算机上使用,而无需更改脚本中的路径。最后,它允许您轻松使用自动生成的 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变量和环境变量的当前值。

    关于python - 使用 GRASS GIS API 的 python 脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062678/

    相关文章:

    python - 在 Python 中序列化/反序列化类对象的最佳方法是什么?

    python - 数据列值未更改为浮点型

    python - 如何仅在一天中的特定时间运行 Python 脚本?

    jquery - apex.submit 不会更改项目值

    java - 我的 postman 发帖请求应该是什么样子? (帖子请求的正文)

    Python - 使用 BeautifulSoup4 的 Reddit 网络爬虫不返回任何内容

    python - Telegram 机器人 Api/Python : Trying to send voice message via my telegram bot

    javascript - 如何知道查询中缺少哪些参数?

    python - 使用 pandas 操作数据框 (Python)

    python - 如何将 AES 与 GAE python 一起使用?