导入错误:无法从 'osgeo' 导入名为 '_gdal_array' 的模块

6
我新建了一个环境,安装了numpy,然后安装了GDAL。GDAL已经成功导入,我可以使用“gdal.Open()”打开图像,但是当我尝试使用“ReadAsRaster”时,出现“ImportError: cannot import name '_gdal_array' from 'osgeo'”错误。
“pip list”返回:
GDAL       3.6.2
numpy      1.24.2
pip        23.0
setuptools 65.6.3
wheel      0.38.4

完全被卡住了,有没有人遇到过这个问题?谷歌告诉我先安装numpy是解决方案(但这并没有帮助)。非常感谢帮助。


也许相关:https://gis.stackexchange.com/questions/83138/cannot-import-gdal-array和https://dev59.com/u6Pia4cB1Zd3GeqP2rf1。两者都暗示如果在安装gdal之后安装了numpy,则可能会出现此类问题。 - rasjani
这个链接 https://gis.stackexchange.com/a/274328 可能会对你有所帮助。 - Dorian Turba
谢谢您的评论,但正如我在问题中提到的那样,我已经尝试过按照这些帖子建议的先安装numpy再安装gdal,但问题仍然存在。 - jester_thomas
1个回答

6

Pip 很可能会缓存并反复重新安装你的错误版本的 GDAL,即使你已经安装了 numpy。这是我的解决方法:

pip3 install --no-cache-dir --force-reinstall 'GDAL[numpy]==3.6.2'

如果不使用 --no-cache-dir 安装,则 pip 会重用已编译的 wheel 包:

% pip3 install --force-reinstall "GDAL[numpy]==3.6.2" 
Collecting GDAL[numpy]==3.6.2
  Using cached GDAL-3.6.2-cp311-cp311-macosx_13_0_x86_64.whl
Collecting numpy>1.0.0
  Using cached numpy-1.24.2-cp311-cp311-macosx_10_9_x86_64.whl (19.8 MB)
Installing collected packages: numpy, GDAL
  Attempting uninstall: numpy
    Found existing installation: numpy 1.24.2
    Uninstalling numpy-1.24.2:
      Successfully uninstalled numpy-1.24.2
  Attempting uninstall: GDAL
    Found existing installation: GDAL 3.6.2
    Uninstalling GDAL-3.6.2:
      Successfully uninstalled GDAL-3.6.2
Successfully installed GDAL-3.6.2 numpy-1.24.2

这对我解决了问题。我之前不知道有关于安装可选依赖的语法 - undefined
安装了wheel后,它对我起作用了。非常感谢。 - undefined

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