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.
There are two parts to GDAL: the GDAL utilities (
gdalinfo
,gdalwarp
, etc.) and the GDAL Python Bindings (when you callfrom 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 thePYTHON_PATH
environment variable.Find the GDAL folder (will contain
gdalwarp
, for instance). Find theosgeo
folder in Python'sLib/site-packages
. Add both to PATH and PYTHON_PATH. You should now be able tofrom 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.
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