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

daz*_*sed 5 python arrays numpy gdal python-3.x

尽管几年前学习了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)
Run Code Online (Sandbox Code Playgroud)

当我使用"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
Run Code Online (Sandbox Code Playgroud)

我是如何安装GDAL或Python的?我应该切换到Python 2.7吗?我已经看到很多关于"没有命名模块"的问题,但是并没有很多涉及GDAL问题的问题.

更新:我只是从我下载的msi重新安装GDAL,它会自动修复它.谁会知道这很容易.谢谢!

Dam*_*ons 3

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

pip3 uninstall gdal
pip3 install numpy
pip3 install gdal
Run Code Online (Sandbox Code Playgroud)

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