Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

python - ImportError: No module named gdal

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:OSGeoW64ingdal_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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...