1

There are multiple solutions on StackOverflow for fixing the

ModuleNotFoundError: No module named '_gdal'

error. However none of them have worked for me. I suspect this may be because I have Python (3.8) installed for the user and GDAL is installed system-wide, and the Python bindings can't import the system-wide gdal module. Before I uninstall Python (and all its packages), I'd like to see if I can't get this configuration to work.

Python and GDAL are operating as expected separately on the command line. I installed GDAL and the python bindings via the GISInternal packages. It did not seem that I had a choice with this installer to install GDAL for the user. It was installed system-wide.

I set the following in both the system and user path environment variables:

C:\Program Files\GDAL\
C:\Users\Admin\AppData\Local\Programs\Python\Python38\Lib\site-packages\osgeo
C:\Users\Admin\AppData\Local\Programs\Python\Python38\Scripts\
C:\Users\Admin\AppData\Local\Programs\Python\Python38\

I set the following environmental variables for the system and then for the user:

GDAL_DATA
C:\Program Files\GDAL\gdal-data


GDAL_DRIVER_PATH
C:\Program Files\GDAL\gdalplugins


PROJ_LIB
C:\Program Files\GDAL\projlib

Based on this advice, I also set

PYTHONPATH
C:\Program Files\GDAL

Even though I am on Windows 10, there was a comment that advised to set this environment variable in Windows as well.

I closed terminal and started a new session, even restarted the computer, and still when I include in my python script:

from osgeo import gdal

I get the error message

Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\osgeo\__init__.py", line 18, in swig_import_helper
fp, pathname, description = imp.find_module('_gdal', [dirname(__file__)])
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\imp.py", line 296, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_gdal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 11, in <module>

from osgeo import gdal
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\osgeo\__init__.py", line 41, in <module>
gdal = swig_import_helper()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\osgeo\__init__.py", line 20, in swig_import_helper
import _gdal
ModuleNotFoundError: No module named '_gdal'

When I look in C:\Users\Admin\AppData\Local\Programs\Python\Python38\Lib\site-packages I see the osgeo directory. There is also a GDAL-3.0.4-py3.7.egg-info directory on that level as well.

interwebjill
  • 920
  • 13
  • 38

0 Answers0