我从官方网站独立安装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.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导入其他一些导入?
答案 0 :(得分: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
目前表示开发版本。对于即将发布的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。首先要注意的是,只有在您真正需要时才这样做。您应该使用的默认解决方案是编写一个将在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门票#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
变量中的空格,反斜杠,目录分隔符和分隔符以及环境变量的当前值。