0

I am trying to do some image analysis in Python but am unable to install GDAL due to limited rights. I have attempted copying some of the files and folders into my python folder but am constantly encountering the same error

ImportError: No module named _gdal

The searching that I have done always seems to point to some sort of installer for GDAL, but I am only able to copy the needed .py folders into my python folder.

Is there a way to do this on Windows and if not is there an alternative image reader that can properly open rasters?

Thank you.

EDIT: sys.path results:

['C:\\Users\\ûser\\Documents\\Python Image Extractor\\Image Interpreter\\src', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\xlutils-1.5.2-py2.6.egg', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\xlrd-0.8.0-py2.6.egg', 'C:\\Python26\\ArcGIS10.0\\DLLs', 'C:\\Python26\\ArcGIS10.0\\lib', 'C:\\Python26\\ArcGIS10.0\\lib\\plat-win', 'C:\\Python26\\ArcGIS10.0\\lib\\lib-tk', 'C:\\Python26\\ArcGIS10.0', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\PIL', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\win32', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\win32\\lib', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\Pythonwin', 'C:\\windows\\system32\\python26.zip', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\bin', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\arcpy', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\ArcToolbox\\Scripts']

3

Ok plan B! Looks like there is a problem with the maptools.org package that does not include the _gdal_array. Sorry took me a while to figure that our. I tend use ogr and not gdal for the most part so I never noticed!

So to start again:

  1. Make sure you have numpy
  2. Create an install directory where all the final bits and pieces will be located. Lets say C:\GDAL. I'll refer to this dir as
  3. Download the appropriate package from: http://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
  4. If you don't have it already get 7-zip, After installing you should be able to right click on the exe and choose extract here. The extract will create two directories DATA and PLATLIB
  5. Copy everything in PLATLIB to
  6. Copy everything in DATA\Lib\site-packages\osgeo to \osgeo

Now its just a matter of altering your PYTHONPATH and PATH environment variables to include your . The following code will do it in a script.

import os
import os.path
import sys

environList = os.environ['PATH'].split(';')
root = r'C:\Kevin\proj\pythonJunkyard\GDALComponents\GDAL-10.1'
environList.insert(0,root)
os.environ['PATH'] = ';'.join(environList)
sys.path.insert(0,root)
import osgeo.ogr
import osgeo.gdal

Hope this works!

  • This fixed it for me. Thank you for your patience and helpfulness. – Dzinic Jan 17 '14 at 13:40
  • I know it's a while later now, but can you please complete point 5 ("Copy everything in PLATLIB to"). Also, I don't see PLATLIB or DATA. Rather, the archive contains directories GDAL-2.1.3.data, GDAL-2.1.3.dist-info, and osgeo. – jbaums May 12 '17 at 3:17
2

Its been a while since I've suffered through this task. Because I'm using the same version of python as the mapserver for windows packages available here (http://www.maptools.org/ms4w/) I've been able to grab that package and then mine out the various pieces I need to create a working gdal/ogr install along with the python bindings! Its likely outdated a bit now, but I did put together a detailed how-to for myself here:

https://sites.google.com/site/bcgeopython/home/ogr-gdal-info/installing-gdal-ogr-version-2

Regarding the _gdal issue. I remember having that problem. I believe it is caused by the gdal binary components not being in the PATH environment variable. You can modify the PATH environment variable programmatically before you attempt to import as is shown here:

import os
environList = os.environ['PATH'].split(';')
environList.insert(0, r'C:\Program Files\gdalwin32-1.6\bin')
os.environ['PATH'] = ';'.join(environList)
import osgeo.ogr
  • Just a question about the environment part of your answer. I seem to be having with _gdal_array and tried using the code you provided but it did not work. Should the line environList.insert(0, r'C:\Program Files\gdalwin32-1.6\bin') be pointing to that location because I do not have anything there due to not being able to install to that directory. – Dzinic Jan 15 '14 at 16:08
  • In the guide I posted above it talks about downloading the gdal ogr binaries that are contained in the maptools.org zip file. (It mentions the need to apply a patch, ignore that as it is no longer required). Then I create a gdal install folder, where I pull various elements from the zip file and put them into sub directories of my gdal install folder. If I remember correctly 3 directories python,tools,plugins. I'd recommend adding them all to your PATH environment variable as described above before trying to import. So the paths to add would be <installdir>/python, <installdir>/tools, ... – Lafleur Jan 15 '14 at 17:40
  • I attempted your solution by adding the directories to my Path folder (through user environment variables) but am still getting the same error.. Do you know where the module _gdal_arrary should be located so I could try and find it? – Dzinic Jan 15 '14 at 17:57
  • Can you show me what the following returns? 'print os.environ['PATH']' and 'print sys.path'? – Lafleur Jan 16 '14 at 17:29
  • print os.environ['PATH'] was too long to store in a comment but it contained C:\GDAL which contains the folders that you told me to use (I use to have a specific one for each folder but it didn`t work either) – Dzinic Jan 16 '14 at 17:56
1

Yes, it's possible. You need to set some environment variables though, either via command prompt/.bat file/os.environ module every time you run your scripts or permanently in Windows.

  1. Download the appropriate stable release from gisinternals.com, i.e. release-1600-gdal-1-10-1-mapserver-6-4-1, select the first download from the package listing (Compiled binaries in a single .zip package)
  2. Unzip the package, rename it to 'gdal'
  3. Run the included SDKShell.bat script to set the appropriate environment variables and start a command prompt from which you may run your python script OR examine the SDKShell.bat script and see which environment variables you need to set permenently, in particular set the PATH, GDAL_DATA, GDAL_DRIVER_PATH, PYTHONPATH and PROJLIB variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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