[GIS] OGR Module Not Found Python

gdalogrpython

I am trying to run a 3.6.1 Python script and one of the libs I need to import is the osgeo. I've already downloaded the GDAL library because I use QGIS. I also have ArcGIS installed on this machine which has Python 2.7.1 installed with it.

When I try to run the script, I get the following error:

Traceback (most recent call last):
  File "C:\Users\username\Desktop\PostgreAccess.py", line 3, in <module>
    from osgeo import ogr
ModuleNotFoundError: No module named 'osgeo'

I don't understand why I'm getting this error. I've already tried adding a new environment variable, restarting my machine, re-downloading the GDAL for Windows. Any suggestions for troubleshooting?

I'm on Windows 10.

Best Answer

The GDAL python bindings must be installed. Also, in non standard python (in you case from ArcGIS) you need manually add path to folder where GDAL bindings placed:

  1. Add to environment variable PYTHONHOME path where osgeo folder situated (not recommended)
  2. Put text file with osgeo folder path named gdal.pth to site-packages of your python installation.

More about #2. For example I have two different python installations:

  1. C:\Python27\
  2. C:\Program Files\nextgis

In first location the site_packages is at path: C:\Python27\Lib\site-packages

In second location the site_packages is at path: C:\Program Files\nextgis\lib\Python27\site-packages

You can copy osgeo folder in both site-packages paths and get GDAL working in both Pythons. Or point to osgeo folder via pth file.

For example you osgeo folder situated at path C:\GDAL\Python\osgeo so you just need to create C:\Python27\Lib\site-packages\gdal.pth and C:\Program Files\nextgis\lib\Python27\site-packages\gdal.pth with one line **C:\GDAL\Python**

Note: The GDAL Python bindings need to find gdal.dll (or gdal220.dll it depends on GDAL version). So this dll mast be in osgeo folder too or path to it mast be in PATH env variable.