Python 3.4 ImportError: No module named '_gdal_array' 没有名为 '_gdal_array' 的模块。

8
尽管几年前我学过Python(2.7),但我现在才开始再次使用它。 我正在使用Python 3.4.4,并尝试使用GDAL包将栅格读取为数组。 我按照此处列出的步骤进行操作:https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windows,但对于Python 3.4,我使用了来自这里的GDAL二进制文件:http://www.gisinternals.com/release.php
在测试基本功能时,我尝试读取一个tiff文件,如下所示。
import gdal as gdal
import numpy as np
import ogr
import osr
import os
import sys
e=('error has occurred')

# this allows GDAL to throw Python Exceptions
gdal.UseExceptions()

# open dataset
test = ('LE70130312004049EDC01_sr_adjacent_cloud_qa.tif')
print("file exists")


# getting metadata
gtif = gdal.Open(test)
print (gtif.GetMetadata())
print("metadata printed")

try:
    src_ds = gdal.Open(test)
    print("gdal.open success")
except (RuntimeError):
    print ('Unable to open INPUT.tif')
    print(e)

try:
    srcband = src_ds.GetRasterBand(1)
    print("get raster band 1 success")
except (RuntimeError):
    # for example, try GetRasterBand(10)
    print ('Band ( %i ) not found') % band_num
    print (e)


try:
    rasArray=np.array(src_ds.ReadAsArray())
    print("read as array")
except (RuntimeError):
    print (e)

当我运行最后一个代码块时,使用"rasArray=np.array(src_ds.ReadAsArray())"命令,会返回以下错误代码:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 16, in swig_import_helper
    fp, pathname, description = imp.find_module('_gdal_array', [dirname(__file__)])
  File "C:\Python34\lib\imp.py", line 297, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_gdal_array'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\dem12002\Desktop\test.py", line 38, in <module>
    rasArray=np.array(src_ds.ReadAsArray())
  File "C:\Python34\lib\site-packages\osgeo\gdal.py", line 1829, in ReadAsArray
    from . import gdalnumeric
  File "C:\Python34\lib\site-packages\osgeo\gdalnumeric.py", line 1, in <module>
    from osgeo.gdal_array import *
  File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 26, in <module>
    _gdal_array = swig_import_helper()
  File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 18, in swig_import_helper
    import _gdal_array
ImportError: No module named '_gdal_array'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\dem12002\Desktop\test.py", line 40, in <module>
    except (RuntimeError,e):
NameError: name 'e' is not defined

这个问题是由于我安装GDAL或Python的方式引起的吗? 我应该切换到Python 2.7吗? 我看到很多关于“没有模块命名”的问题,但是没有太多涉及像GDAL这样的问题。

更新: 我只需重新安装我下载的msi中的GDAL,它就自动修复了。谁知道这么简单。谢谢!


显然,与GDAL的接口是用SWIG编写的。首先检查您安装的版本是否与您的Python版本兼容。该模块正在寻找一个名为'_gdal_array'的dll,并带有前导下划线,但找不到它。您可以从文件系统搜索开始查找是否有此dll。 - Gribouillis
谢谢,我注意到在site-packages/osgeo/目录下有一个名为"gdal_array"的Python文件,但不知道要查找dll文件。 - dazed but not confused
Gribouillis,我发现osgeo文件夹中缺少_gdal_array.pyd文件,你有什么建议吗? - dazed but not confused
如果你有一个 _gdal_array.dll 文件,你可以尝试将它重命名为 _gdal_array.pyd。很抱歉我无法告诉你更多的信息,因为我不熟悉这个模块。我很久以前在 Linux 上使用过 Swig。 - Gribouillis
1个回答

3

我也遇到过这个问题,以下是我的解决方法:

pip3 uninstall gdal
pip3 install numpy
pip3 install gdal

顺便说一下,我正在使用MAC OS操作系统


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接