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
1.0k views
in Technique[技术] by (71.8m points)

can't import gdal in python?

I have gdal installed and running on Ubuntu Jaunty but I can't run gdal2tiles because I get the error:

Traceback (most recent call last):
  File "/usr/local/bin/gdal2tiles.py", line 42, in <module>
    import gdal
ImportError: No module named gdal

When I open python and type import gdal I get the same error.

I've set LD_LIBRARY_PATH (without spaces!) to /usr/local/lib but it doesn't seem to have made any difference.

Looks like Python can't find gdal. Can anyone help?

Thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Ith seems to be a "Python Path" issue.  Python libraries are looked-up within a defined path. Try

import sys
sys.path

If the directory where the gdal.py and related files is not in this list, then Python cannot find it. That's for the "diagnostics" part. To fix the situation, you have several options... They all hinge on knowing the rules which Python uses to build this path.

  • The PYTHONPATH environement variable can be altered to include the desired path
  • Python can be started from the directory where the desired library resides.
  • sys.path can be dynamically altered, with sys.path.append("/SomePath/To/MyStuff")

The only place where I've seen the rules pertaining to the building of sys.path formerly described, within the "official" Python documentation, is in the tutorial, at section 6.1.2
Excerpt:

modules are searched in the list of directories given by the variable sys.path
which is initialized from the directory containing the input script (or the 
current directory), PYTHONPATH and the installation- dependent default. This
allows Python programs that know what they’re doing to modify or replace the
module search path. Note that because the directory containing the script being
run is on the search path, it is important that the script not have the same name
as a standard module, or Python will attempt to load the script as a module when
that module is imported. This will generally be an error. See section
Standard Modules for more information.

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

...