导入错误:无法从“osgeo”导入名称“_gdal_array”

jes*_*mas 7 python gdal

我创建一个新的环境,安装 numpy,然后安装 GDAL。GDAL 导入成功,我可以使用打开图像,但在尝试使用时gdal.Open(出现错误。ImportError: cannot import name '_gdal_array' from 'osgeo'ReadAsRaster

pip list返回:

GDAL       3.6.2
numpy      1.24.2
pip        23.0
setuptools 65.6.3
wheel      0.38.4
Run Code Online (Sandbox Code Playgroud)

彻底迷惑了,有人遇到过这种情况吗?谷歌告诉我,首先安装 numpy 是解决方案(但这没有帮助)。非常感谢您的帮助。

Wil*_*ill 9

即使您安装了 numpy,Pip 也很可能会一遍又一遍地缓存和重新安装您的错误版本的 GDAL。这是为我解决的问题:

\n

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

\n

安装时不会--no-cache-dir导致 pip 重用已编译的wheel:

\n
% pip3 install --force-reinstall \'GDAL[numpy]==3.6.2\'\nCollecting GDAL[numpy]==3.6.2\n  Using cached GDAL-3.6.2-cp311-cp311-macosx_13_0_x86_64.whl\nCollecting numpy>1.0.0\n  Using cached numpy-1.24.2-cp311-cp311-macosx_10_9_x86_64.whl (19.8 MB)\nInstalling collected packages: numpy, GDAL\n  Attempting uninstall: numpy\n    Found existing installation: numpy 1.24.2\n    Uninstalling numpy-1.24.2:\n      Successfully uninstalled numpy-1.24.2\n  Attempting uninstall: GDAL\n    Found existing installation: GDAL 3.6.2\n    Uninstalling GDAL-3.6.2:\n      Successfully uninstalled GDAL-3.6.2\nSuccessfully installed GDAL-3.6.2 numpy-1.24.2\n
Run Code Online (Sandbox Code Playgroud)\n
\n

对于仍然遇到此问题的其他人,请确保wheel已安装。

\n

Pip 将wheel作为构建过程的一部分进行下载,但它无法工作(截至 2023 年 6 月),而是归咎于 numpy:

\n
Building wheels for collected packages: GDAL\n  Running command Building wheel for GDAL (pyproject.toml)\n  WARNING: numpy not available!  Array support will not be enabled\n  running bdist_wheel\n
Run Code Online (Sandbox Code Playgroud)\n

唯一表明存在wheel问题的迹象是在没有构建隔离的情况下构建时出现的 ( --no-build-isolation):

\n
  error: invalid command \'bdist_wheel\'\n  error: subprocess-exited-with-error\n  \n  \xc3\x97 Preparing metadata (pyproject.toml) did not run successfully.\n  \xe2\x94\x82 exit code: 1\n  \xe2\x95\xb0\xe2\x94\x80> See above for output.\n
Run Code Online (Sandbox Code Playgroud)\n