ImportError: No module named gdal
Asked Answered
F

3

19

My GDAL is showing strange behaviour when I try to use ".py" functions from the command line:

1. For example, when I run gdalinfo --version, I get the standard response:

GDAL 1.11.3, released 2015/09/16

2. Also when I run gdalwarp, I get the standard response showing me the flags and everything.

3. Problem: However, when I run gdal functions with a .py extension, for example gdal_polygonize.py, I get the following:

Traceback <most recent call last>:
    File "C:\OSGeoW64\bin\gdal_polygonize.py", line 36, in <module>
    import gdal, ogr, osr
ImportError: No module named gdal

I checked my Path variable, reinstalled python and gdal, but nothing worked. When I run the gdal_polygonize.py function from my QGIS it works. I just can`t use it from my command line.

Flexuosity answered 19/2, 2016 at 7:57 Comment(7)
make sure your PYTHONPATH is set to the place you installed the library.Greater
@Greater : I guess you mean the folder that contains the gdal_polygonize.py function? In my case thats C:\OSGeo4W64\bin, but it´s still not working.Flexuosity
try printing out sys.path and check whether gdal resides thereMaypole
@samera : sys.path returns a couple of directories and also "C:\OSGeo4W64\bin"Flexuosity
No I mean the folder that contains the gdal library.Greater
@Greater : on windows that would for example be: "C:\gdalwin32-1.5\bin" right? Or am I mixing somehting up?Flexuosity
could be the case. I have actually no idea what gdal isGreater
C
16

There are two parts to GDAL: the GDAL utilities (gdalinfo, gdalwarp, etc.) and the GDAL Python Bindings (when you call from osgeo import gdal from within a Python script.). The two can be (or could be in the past) installed separately.

The fact that you "see" gdalwarp on the command line means that you have the location of the utilities on your PATH (environment variables). Some of the utilities are self-contained, some require the Python bindings (notably those that require calling a .py file). In order for the Python bindings to work, GDAL has to be on the PYTHONPATH environment variable.

Find the GDAL folder (will contain gdalwarp, for instance). Find the osgeo folder in Python's Lib/site-packages. Add both of these locations to PATH and PYTHONPATH. You should now be able to from osgeo import gdal in a fresh Python shell.

If you cannot, either you are missing some files in those locations (bad install), or you have not specified the path correctly / set the right environment variables. There's no other magic involved, it's either installed and linked correctly, or you did something wrong.

Note that for environment variables to be set and working, you need to "Apply/OK" the environement variables window(s), and also start a fresh command prompt / Python shell.

Cadal answered 7/2, 2017 at 15:31 Comment(0)
G
0

Add the folder path of 'bin' and 'site-packages' to the PATH variable as the following:

os.environ['PATH'] += ";D:\\hxl170008\\Conda_Env\\env_ortho\\Library\\bin"
os.environ['PATH'] += ";D:\\hxl170008\\Conda_Env\\env_ortho\\Lib\\site-packages"
Gulch answered 19/6, 2024 at 8:15 Comment(0)
B
-5

make sure you install the conda version of gdal. I never really got gdal to work properly until I installed it from conda. Make sure you have the anaconda python distribution. https://www.continuum.io/downloads

Baziotes answered 9/2, 2017 at 13:46 Comment(1)
Conda is only one of many choices, and its not the standard (PyPy repos [pip etc] are the standard). The pip version works just fine.Cooksey

© 2022 - 2025 — McMap. All rights reserved.