我从官方网站上独立安装了GRASS,我使用python2.732b和windows 10,我尝试使用来自GRASS GIS 7.0.4的python。但我有导入错误。
有些导入工作我可以在python中导入,而有些导入不起作用,我无法使用。
首先,我阅读了这篇文章grass python,并尝试使用pygrass 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
你知道为什么我可以从豆科植物进口一些进口品,为什么我不能从绿草中进口一些其他的进口品?
发布于 2016-06-28 17:50:16
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。看起来你在用一些过时的东西(错了吗?)文档。最新的文件如下:
grass73当前表示开发版本。对即将发布的7.2版使用grass72,对当前的7.0版使用grass70 (7.0.4)。
所有其他错误都是ImportError: grass_gis.7.0.4 not found,它与前面的行(例如ctypes_loader.py...load_library)有关,这表明问题在于使用C类型加载C动态库时没有发现C类型。
您似乎试图在Python中从GRASS会话之外使用GRASS GIS,即不显式地启动GRASS GIS。首先,请注意,这是好的,只有当你真正需要它。您应该使用的默认解决方案是编写一个Python脚本,它将在GRASS会话中运行(即启动GRASS GIS,然后运行脚本)。您可以从菜单File > Run script或命令行执行此操作。开发版本甚至包含一个简单的Python编辑器,它具有特定于GRASS GIS的一些特殊功能。编写在GRASS会话中运行的脚本可以消除在脚本中设置环境的需要,您只需专注于功能即可。它还将使它更容易在不同的计算机上使用,而不改变脚本中的路径。最后,它允许您轻松地使用诸如自动生成的GUI之类的东西。如果您想在不使用GRASS的情况下自动/以编程方式启动脚本,您可以查看grass (grass7,grass70,.)命令help提供并选择将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依赖于通过ctype使用并在导入过程中加载的动态库。操作系统使用其设置和当前环境变量搜索库。对于动态库,它使用从进程启动时起的环境变量。因此,在脚本的后面修改它,例如grass.script.setup.init(),并不会影响进程本身,而只会影响它的子进程。因此,您需要为Linux执行如下命令:
export LD_LIBRARY_PATH=$(grass70 --config path)/lib
python a_python_script.py请参阅GRASS地理信息系统票#2424中的更深入讨论:
https://trac.osgeo.org/grass/ticket/2424
在mixed上,库的路径存储在PATH变量中(与可执行文件的路径混合),所以在那里使用PATH。你需要这样做:
set PATH="%PATH%;C:\Program Files (x86)\GRASS GIS 7.0.4\lib"您还可以在MS注册表中永久地更改PATH变量(例如,使用xset)。网上有很多关于这方面的资料(对任何程序来说都是一样的)。
此外,在处理Python中的路径时,我建议使用os.path.join,因为在MS上处理文件路径有一些细节。有关详细信息,请参阅Python文档:
https://docs.python.org/2/library/os.path.html#os.path.join
这同样适用于构造PATH变量和其他包含路径的变量。通常(在命令行或Python中),注意PATH/LD_LIBRARY_PATH变量中的空格、反斜杠、目录分隔符和分隔符以及环境变量的当前值。
https://stackoverflow.com/questions/38062678
复制相似问题