[gdal-dev] Python installation can't find gdal_array.py

Jorge Arévalo jorge at cartodb.com
Wed Jan 28 10:36:04 PST 2015



> Even Rouault <mailto:even.rouault at spatialys.com>
> January 28, 2015 at 7:25 PM
> Le mercredi 28 janvier 2015 19:08:18, Jorge Arévalo a écrit :
>>> Even Rouault<mailto:even.rouault at spatialys.com>
>>> January 28, 2015 at 6:53 PM
>>>
>>> Le mercredi 28 janvier 2015 18:51:28, Jorge Arévalo a écrit :
>>>> Hi,
>>>>
>>>>> Even Rouault<mailto:even.rouault at spatialys.com>
>>>>> January 28, 2015 at 6:44 PM
>>>>> Jorge,
>>>>>
>>>>>> I've compiled GDAL from trunk, specifying --with-python at configure
>>>>>> time. I first checked numpy is correctly installed. The problem is I
>>>>>> couldn't import gdal_array.py by default (got ImportError: no module
>>>>>> named _gdal_array).
>>>>>>
>>>>>> Tried
>>>>>>
>>>>>> python gdal_sources/swig/python/setup.py install
>>>>>>
>>>>>> Didn't work.
>>>>> "didn't work", could you be more precise ?
>>>>> "python setup.py install" requires root rights since it installs in
>>>>> /usr/local/lib. There's a ticket&debate if the python bindings should
>>>>> honour the --prefix specified at configure time or not. Currently they
>>>>> don't, and it is an annoyance for folks. Perhaps we should revert to
>>>>> honour the prefix, but this was disabled at some point and nobody
>>>>> remembers why...
>>>> Yes, I felt the pain of python bindings don't honouring the --prefix
>>>> specified... Good to know there's a debate on this.
>>>>
>>>> Sorry about my vague description. I just did it using sudo, for a system
>>>> installation, and after that, system Python still didn't find the path.
>>>> So, after
>>>>
>>>> sudo python gdal_sources/swig/python/setup.py install
>>>>
>>>> I still get the same error:
>>>>
>>>> ImportError: no module named _gdal_array
>>> Are you sure you don't have a python-gdal package hanging somewhere ?
>>> (but that would be weird it wouldn't have numpy support). Or a custom
>>> python-gdal bindings installed in another location that is tried before
>>> the one where you install.
>> Yep. Looked for gdal packages:
>>
>> dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' |
>> sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n",
>> $2/(1024), $1}' | grep gdal
>>
>> 0 found. Just installed GDAL from sources, using this line
>>
>> ./configure --with-python --without-libtool
>> --with-curl=/usr/bin/curl-config
>>
>> Also looked for any existent gdal_array.py
>>
>> sudo find / -name gdal_array.py
>>
>> Just found the one in my sources tree, and the system one at
>> /usr/local/lib/python2.7/dist-packages/osgeo/gdal_array.py.
>>
>> If I print sys.path from Python console, I get:
>>
>> ['', '/home/jorge', '/usr/lib/python2.7',
>> '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk',
>> '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',
>> '/usr/local/lib/python2.7/dist-packages',
>> '/usr/lib/python2.7/dist-packages',
>> '/usr/lib/python2.7/dist-packages/PILcompat']
>
> Trying the same with python2.6, I have the following in sys.path :
> '/usr/local/lib/python2.6/dist-packages/GDAL-2.0.0-py2.6-linux-x86_64.egg'
>
> And
>
> 'from osgeo import gdal_array' works without PYTHONPATH defined
>
> What is the output of "python setup.py install" ? Mine for reference :
>
> $ sudo python setup.py install
>
> running install
> running bdist_egg
> running egg_info
> creating GDAL.egg-info
> writing GDAL.egg-info/PKG-INFO
> writing top-level names to GDAL.egg-info/top_level.txt
> writing dependency_links to GDAL.egg-info/dependency_links.txt
> writing manifest file 'GDAL.egg-info/SOURCES.txt'
> writing manifest file 'GDAL.egg-info/SOURCES.txt'
> installing library code to build/bdist.linux-x86_64/egg
> running install_lib
> running build_py
> running build_ext
> creating build/bdist.linux-x86_64
> creating build/bdist.linux-x86_64/egg
> copying build/lib.linux-x86_64-2.6/gdal.py ->  build/bdist.linux-x86_64/egg
> creating build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/gdal.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/_gdalconst.so ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/__init__.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/__init__.pyc ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/gdalconst.pyc ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/_gdal.so ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/ogr.pyc ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/gdalnumeric.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/_gdal_array.so ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/gdalconst.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/ogr.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/osr.pyc ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/gdal_array.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/gdal.pyc ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/osr.py ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/_ogr.so ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/osgeo/_osr.so ->  build/bdist.linux-x86_64/egg/osgeo
> copying build/lib.linux-x86_64-2.6/gdalnumeric.py ->  build/bdist.linux-x86_64/egg
> copying build/lib.linux-x86_64-2.6/gdalconst.py ->  build/bdist.linux-x86_64/egg
> copying build/lib.linux-x86_64-2.6/ogr.py ->  build/bdist.linux-x86_64/egg
> copying build/lib.linux-x86_64-2.6/osr.py ->  build/bdist.linux-x86_64/egg
> byte-compiling build/bdist.linux-x86_64/egg/gdal.py to gdal.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/gdalnumeric.py to gdalnumeric.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/gdal_array.py to gdal_array.pyc
> byte-compiling build/bdist.linux-x86_64/egg/gdalnumeric.py to gdalnumeric.pyc
> byte-compiling build/bdist.linux-x86_64/egg/gdalconst.py to gdalconst.pyc
> byte-compiling build/bdist.linux-x86_64/egg/ogr.py to ogr.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osr.py to osr.pyc
> creating stub loader for osgeo/_gdal.so
> creating stub loader for osgeo/_gdalconst.so
> creating stub loader for osgeo/_osr.so
> creating stub loader for osgeo/_ogr.so
> creating stub loader for osgeo/_gdal_array.so
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/_gdal.py to _gdal.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/_gdalconst.py to _gdalconst.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/_osr.py to _osr.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/_ogr.py to _ogr.pyc
> byte-compiling build/bdist.linux-x86_64/egg/osgeo/_gdal_array.py to _gdal_array.pyc
> creating build/bdist.linux-x86_64/egg/EGG-INFO
> copying GDAL.egg-info/PKG-INFO ->  build/bdist.linux-x86_64/egg/EGG-INFO
> copying GDAL.egg-info/SOURCES.txt ->  build/bdist.linux-x86_64/egg/EGG-INFO
> copying GDAL.egg-info/dependency_links.txt ->  build/bdist.linux-x86_64/egg/EGG-INFO
> copying GDAL.egg-info/not-zip-safe ->  build/bdist.linux-x86_64/egg/EGG-INFO
> copying GDAL.egg-info/top_level.txt ->  build/bdist.linux-x86_64/egg/EGG-INFO
> writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
> creating dist
> creating 'dist/GDAL-2.0.0-py2.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
> removing 'build/bdist.linux-x86_64/egg' (and everything under it)
> Processing GDAL-2.0.0-py2.6-linux-x86_64.egg
> creating /usr/local/lib/python2.6/dist-packages/GDAL-2.0.0-py2.6-linux-x86_64.egg
> Extracting GDAL-2.0.0-py2.6-linux-x86_64.egg to /usr/local/lib/python2.6/dist-packages
> Adding GDAL 2.0.0 to easy-install.pth file
>
> Installed /usr/local/lib/python2.6/dist-packages/GDAL-2.0.0-py2.6-linux-x86_64.egg
> Processing dependencies for GDAL==2.0.0
> Finished processing dependencies for GDAL==2.0.0
>
Mine is much shorter

running install
running build
running build_py
running build_ext
running install_lib
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/GDAL-2.0.0.egg-info
Writing /usr/local/lib/python2.7/dist-packages/GDAL-2.0.0.egg-info



>> So, /usr/local/lib/python2.7/dist-packages is included, but
>> /usr/local/lib/python2.7/dist-packages/osgeo doesn't. I tried with
>>
>> from osgeo import gdal_array
>>
>> And then, I get ImportError: no module named _gdal
>>
>>>>>> I finally had to define
>>>>>>
>>>>>> export
>>>>>> PYTHONPATH=$PYTHONPATH:$/usr/local/lib/python2.7/dist-packages/osgeo
>
> Should rather be PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages (without the final osgeo) actually

Actually, that path is already included

['', '/home/jorge', '/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat']

So, if I try something like

from osgeo import gdal

it works. But running gdal_merge.py, for example, I get the mentioned error

   File "/usr/local/bin/gdal_merge.py", line 513, in <module>
     sys.exit(main())
   File "/usr/local/bin/gdal_merge.py", line 499, in main
     fi.copy_into( t_fh, band, band, nodata )
   File "/usr/local/bin/gdal_merge.py", line 243, in copy_into
     nodata_arg )
   File "/usr/local/bin/gdal_merge.py", line 63, in raster_copy
     nodata )
   File "/usr/local/bin/gdal_merge.py", line 99, in raster_copy_with_nodata
     t_xsize, t_ysize )
   File "/usr/local/lib/python2.7/dist-packages/osgeo/gdal.py", line 
1331, in ReadAsArray
     import gdalnumeric
   File "/usr/local/lib/python2.7/dist-packages/osgeo/gdalnumeric.py", 
line 1, in <module>
     from gdal_array import *
   File "/usr/local/lib/python2.7/dist-packages/osgeo/gdal_array.py", 
line 25, in <module>
     _gdal_array = swig_import_helper()
   File "/usr/local/lib/python2.7/dist-packages/osgeo/gdal_array.py", 
line 17, in swig_import_helper
     import _gdal_array
ImportError: No module named _gdal_array

Opening gdal_merge.py, I see the imports are doing in the right way:

try:
     from osgeo import gdal
except ImportError:
     import gdal

I'd understand if an old version of the script still does

import gdal

which is deprecated way to import gdal bindings (right?). But the script 
is also from trunk. The last version.



>
>>>>>> to make it work. And I don't think that's the default solution, right?
>>>>> That's what I use since I have many GDAL versions on my machine and
>>>>> must switch between them without reinstalling. For regular users not
>>>>> tweaking the installation prefix, "sudo make install" should do the
>>>>> right thing however.
>>>> Yep, that was what I thought. But still fails after sudo make install.
>>>> The only way I've found to make it work is by exporting PYTHONPATH
>>>>
>>>> Best regards,
>>>>
>>>> Jorge
>>>>
>>>>>> Am I doing something wrong?
>>>>>>
>>>>>> Environment: Ubuntu 14.10, GDAL from trunk, Python 2.7.8
>>>>> Even
>>> Even Rouault<mailto:even.rouault at spatialys.com>
>>> January 28, 2015 at 6:44 PM
>>> Jorge,
>>>
>>>> I've compiled GDAL from trunk, specifying --with-python at configure
>>>> time. I first checked numpy is correctly installed. The problem is I
>>>> couldn't import gdal_array.py by default (got ImportError: no module
>>>> named _gdal_array).
>>>>
>>>> Tried
>>>>
>>>> python gdal_sources/swig/python/setup.py install
>>>>
>>>> Didn't work.
>>> "didn't work", could you be more precise ?
>>> "python setup.py install" requires root rights since it installs in
>>> /usr/local/lib. There's a ticket&debate if the python bindings should
>>> honour the --prefix specified at configure time or not. Currently they
>>> don't, and it is an annoyance for folks. Perhaps we should revert to
>>> honour the prefix, but this was disabled at some point and nobody
>>> remembers why...
>>>
>>>> I finally had to define
>>>>
>>>> export
>>>> PYTHONPATH=$PYTHONPATH:$/usr/local/lib/python2.7/dist-packages/osgeo
>>>>
>>>> to make it work. And I don't think that's the default solution, right?
>>> That's what I use since I have many GDAL versions on my machine and must
>>> switch between them without reinstalling. For regular users not tweaking
>>> the installation prefix, "sudo make install" should do the right thing
>>> however.
>>>
>>>> Am I doing something wrong?
>>>>
>>>> Environment: Ubuntu 14.10, GDAL from trunk, Python 2.7.8
>>> Even
>
> Even Rouault <mailto:even.rouault at spatialys.com>
> January 28, 2015 at 6:53 PM
> Le mercredi 28 janvier 2015 18:51:28, Jorge Arévalo a écrit :
>> Hi,
>>
>>> Even Rouault<mailto:even.rouault at spatialys.com>
>>> January 28, 2015 at 6:44 PM
>>> Jorge,
>>>
>>>> I've compiled GDAL from trunk, specifying --with-python at configure
>>>> time. I first checked numpy is correctly installed. The problem is I
>>>> couldn't import gdal_array.py by default (got ImportError: no module
>>>> named _gdal_array).
>>>>
>>>> Tried
>>>>
>>>> python gdal_sources/swig/python/setup.py install
>>>>
>>>> Didn't work.
>>> "didn't work", could you be more precise ?
>>> "python setup.py install" requires root rights since it installs in
>>> /usr/local/lib. There's a ticket&debate if the python bindings should
>>> honour the --prefix specified at configure time or not. Currently they
>>> don't, and it is an annoyance for folks. Perhaps we should revert to
>>> honour the prefix, but this was disabled at some point and nobody
>>> remembers why...
>> Yes, I felt the pain of python bindings don't honouring the --prefix
>> specified... Good to know there's a debate on this.
>>
>> Sorry about my vague description. I just did it using sudo, for a system
>> installation, and after that, system Python still didn't find the path.
>> So, after
>>
>> sudo python gdal_sources/swig/python/setup.py install
>>
>> I still get the same error:
>>
>> ImportError: no module named _gdal_array
>
> Are you sure you don't have a python-gdal package hanging somewhere ? (but
> that would be weird it wouldn't have numpy support). Or a custom python-gdal
> bindings installed in another location that is tried before the one where you
> install.
>
>>>> I finally had to define
>>>>
>>>> export
>>>> PYTHONPATH=$PYTHONPATH:$/usr/local/lib/python2.7/dist-packages/osgeo
>>>>
>>>> to make it work. And I don't think that's the default solution, right?
>>> That's what I use since I have many GDAL versions on my machine and must
>>> switch between them without reinstalling. For regular users not tweaking
>>> the installation prefix, "sudo make install" should do the right thing
>>> however.
>> Yep, that was what I thought. But still fails after sudo make install.
>> The only way I've found to make it work is by exporting PYTHONPATH
>>
>> Best regards,
>>
>> Jorge
>>
>>>> Am I doing something wrong?
>>>>
>>>> Environment: Ubuntu 14.10, GDAL from trunk, Python 2.7.8
>>> Even
>

-- 
Sent with Postbox <http://www.getpostbox.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20150128/033b1002/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compose-unknown-contact.jpg
Type: image/jpeg
Size: 770 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20150128/033b1002/attachment-0001.jpg>


More information about the gdal-dev mailing list