ImportError: cannot import name _gdal_array from osgeo

2024/11/15 10:44:10

I create a fresh environment, install numpy, then install GDAL. GDAL imports successfully and I can open images using gdal.Open(, but I get the ImportError: cannot import name '_gdal_array' from 'osgeo' error when trying to use ReadAsRaster.

pip list returns:

GDAL       3.6.2
numpy      1.24.2
pip        23.0
setuptools 65.6.3
wheel      0.38.4

Completely stumped, has anyone come across this? Google tells me that installing numpy first is the solution (but that doesn't help). Help would be much appreciated.

Answer

Pip is most likely caching and reinstalling your bad version of GDAL over and over, even though you installed numpy. Here's what fixed it for me:

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

Installing without --no-cache-dir causes pip to reuse the compiled wheel:

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

For others still encountering this issue, make sure wheel is installed.

Pip will download wheel as part of the building process, but it will not work (as of June 2023) and instead blame numpy:

Building wheels for collected packages: GDALRunning command Building wheel for GDAL (pyproject.toml)WARNING: numpy not available!  Array support will not be enabledrunning bdist_wheel

The only indication that it's a wheel problem shows up when building without build isolation (--no-build-isolation):

  error: invalid command 'bdist_wheel'error: subprocess-exited-with-error× Preparing metadata (pyproject.toml) did not run successfully.│ exit code: 1╰─> See above for output.
https://en.xdnf.cn/q/71968.html

Related Q&A

How do I insert a map into DynamoDB table?

I have the following line of code :table.put_item( Item={filename : key, status : {M : iocheckdict }})The iocheckdict looks like this:{A: One, C: Three, D: Four, B: Two, E: Five}So, when I am running t…

How to redirect django.contrib.auth.views.login after login?

I added django.contrib.auth.views.login everywhere in my webpage, for that I had to load a templatetag (that returns the AuthenticationForm) in my base.html. This templatetags includes the registration…

How to do windows API calls in Python 3.1?

Has anyone found a version of pywin32 for python 3.x? The latest available appears to be for 2.6.Alternatively, how would I "roll my own" windows API calls in Python 3.1?

Returning the outputs from a CloudFormation template with Boto?

Im trying to retrieve the list of outputs from a CloudFormation template using Boto. I see in the docs theres an object named boto.cloudformation.stack.Output. But I think this is unimplemented functi…

numpy.array of an I;16 Image file

I want to use TIFF images to effectively save large arrays of measurement data. With setting them to mode="I;16" (corresponding to my 16 bit data range), they yield 2MB files (~1000x1000 &quo…

Namespace packages and pip install -e

I have a ns.pkg2 package that depends on ns.pkg1 package. I make a fork of it, publish it to git and want to install my version into my virtualenv. I use pip install -e mygit and end up with ns.pkg in …

Python sys.argv out of range, dont understand why

I have a script that Ive been using for a some time to easily upload files to my server. It has been working great for a long time, but I cant get it to work on my new desktop computer. The code is sim…

Error calling BashOperator: Bash command failed

Here are my dag file and BashOperator task:my_dag = { dag_id = my_dag, start_date = datetime(year=2017, month=3, day=28), schedule_interval=01***, }my_bash_task = BashOperator( task_id="my_bash_t…

Match unescaped quotes in quoted csv

Ive looked at several of the Stack Overflow posts with similar titles, and none of the accepted answers have done the trick for me.I have a CSV file where each "cell" of data is delimited by …

Creating RDF file using csv file as input

I need to convert a csv file to rdf with rdflib, I already have the code that reads the csv but I do not know how to convert it to rdf.I have the following code:import csv from rdflib.graph import Grap…