24

I am using python 2.7 and have gdal installed. I want to read an entire .jpg image at once and so I used statement

import gdal
from gdalconst import *
driver = gdal.GetDriverByName('JPEG')
driver.Register()
dataset = gdal.Open('1.jpg', GA_ReadOnly)

but I am getting an import error for gdal_array.

ImportError                               Traceback (most recent call last)
<ipython-input-7-6a16047a395b> in <module>()
----> 1 data = band.ReadAsArray(0, 0, cols, rows)

C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdal.pyc in ReadAsArray(self, xoff,        yoff, win_xsize, win_ysize, buf_xsize, buf_ysize, buf_obj)
1174     def ReadAsArray(self, xoff=0, yoff=0, win_xsize=None, win_ysize=None,
1175                     buf_xsize=None, buf_ysize=None, buf_obj=None):
-> 1176         import gdalnumeric
1177 
1178         return gdalnumeric.BandReadAsArray( self, xoff, yoff,

C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdalnumeric.py in <module>()
----> 1 from gdal_array import *
2 from numpy import *

C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdal_array.py in <module>()
 24                 fp.close()
 25             return _mod
 ---> 26     _gdal_array = swig_import_helper()
 27     del swig_import_helper
 28 else:

 C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdal_array.py in swig_import_helper()
 16             fp, pathname, description = imp.find_module('_gdal_array', [dirname(__file__)])
 17         except ImportError:
 ---> 18             import _gdal_array
 19             return _gdal_array
 20         if fp is not None:

 ImportError: No module named _gdal_array

Please tell me what could possible be going wrong. I am new to using GDAL.

1
  • I meet the same problem when using python 3.4 I am puzzled that when I changed to X86(32bit) version ,then it could run. expect someone to solve the probelm in x64 version. Commented Dec 6, 2016 at 11:09

7 Answers 7

36

Make sure you have numpy installed before you attempt to install the GDAL Python bindings; without numpy, it appears the _gdal_array native code will not be installed.

If you ended up in this situation, removing gdal, installing numpy and then reinstalling gdal might help:

    pip uninstall gdal

    pip install numpy

    pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal"
2
  • 1
    This worked. Just to be clear I only needed to reinstall the GDAL Python bindings after Numpy, and did not need a full GDAL build. My problem was I had Numpy on my system but was running GDAL in a virtualenv which could not find the system Numpy.
    – Pete
    Commented Dec 7, 2020 at 22:44
  • 2
    I also ran into issues where the bindings were installed from a binary wheel. Adding the flag --no-binary :all: or --no-cache-dir made Pip skip the wheel and package the gdal bindings locally, which addressed my problems.
    – Pete
    Commented Dec 7, 2020 at 23:09
7

Things are a little different in 2023, since build isolation was introduced. Pip will now build packages prior to installation within an isolated Python environment, so even if numpy is installed in your working environment, it's still not found.

The steps that worked for me were to disable build isolation -

pip uninstall gdal

# ensure numpy is installed prior to installing gdal
pip install numpy

# ensure setuptools and wheel are installed to do the build in your current environment
pip install -U setuptools wheel

# install gdal
pip install --no-build-isolation --no-cache-dir --force-reinstall gdal 

p.s. the way to fix this properly would be for the GDAL python package to add a pyproject.toml and add numpy (or oldest-supported-numpy) to the build-system.requires field.

2

I ran across the same problem and it appears that many others have too when using the Anaconda environment. The accepted answer here did not work for me.

However, I solved it by working my way throught the alternative instalation methods listed in the Anaconda GDAL instalation documentation for the conda-forge package distributions. The last option (conda install -c conda-forge/label/cf201901 gdal) finally gave me a working environment with gdal_array. It also updated/reinstalled a ton of other packages too so be patient as it gathers information. It took a few minutes.

I don't think you need to try all of them like I did - I guess you can jump to the last one. I also suspect that the label may change over time - so I would check the documentation in the link before just cutting and pasting the command above if you look at this post a few months or years after I write this!

1

I had the same problem and solved it by building gdal from scratch, after I had Python 2.7 with numpy installed.

1

I am using gdal within a conda virtual environment. I had the same error, and I was able to resolve it by uninstalling and re-installing some of gdal's key dependencies.

To determine its dependencies: conda info gdal

More specifically, I had to conda uninstall the following:

  • gdal
  • numpy
  • libgdal
  • geos

Quick note on geos: it was not listed as a dependency upon querying with conda info. However, when attempting to reinstall gdal, I had found that there were conflicting libraries associated with geos, so I uninstalled it for thoroughness.

Then, a quick conda install gdal will re-install gdal and its dependencies. When you re-install gdal again with the above line, double-check that you are installing gdal version > 2.0

These specific commands will be different depending on your python distribution / package manager. But hopefully, following a similar procedure of uninstalling/reinstalling will enable you to get a working version.

0

Add this to the script: from osgeo import gdal_array

-1

I just solved this by placing the right files extracted from RPM. Here is where I get my 64bit RPM http://www.filewatcher.com/d/Mandriva/2010.1/x86_64/Sciences/Geosciences/gdal-python-1.7.2-1mdv2010.1.x86_64.rpm.220418.html

Using the rpm extraction command provided by http://www.cyberciti.biz/tips/how-to-extract-an-rpm-package-without-installing-it.html

I was able to find the lost _gdal_array.* files in the path below usr/lib64/python2.6/site-packages/GDAL-1.7.2-py2.6-linux-x86_64.egg/osgeo/

Then simply copy _gdal_array.* to python2.7/site-packages/osgeo/ and problem solved. Hope this helps anyone with similar problems.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.