如何在Python中导入gdal?

2024-04-26 05:06:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在Ubuntu Jaunty上安装并运行了gdal,但是我无法运行gdal2tiles,因为我收到错误:

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

当我打开python并键入import gdal时,会得到相同的错误。

我已经set LD_LIBRARY_PATH(没有空格!)但似乎没有什么不同。

看起来Python找不到gdal。有人能帮忙吗?

谢谢!


Tags: importmostubuntuusrlocal错误callfile
3条回答

这里有一个链接,教您如何安装和使用Python+GDAL、OGR和其他工具。

我希望它有用。

http://www.gis.usu.edu/~chrisg/python/2009/

Ith似乎是一个“Python Path”问题。Python库在定义的路径中查找。试试看

import sys
sys.path

如果gdal.py和相关文件所在的目录不在此列表中,则Python找不到它。那是“诊断”部分。要解决这个问题,你有几个选择。。。它们都依赖于了解Python用来构建此路径的规则。

  • PYTHONPATH环境变量可以更改为包含所需的路径
  • Python可以从所需库所在的目录启动。
  • 可以使用sys.path.append(“/SomePath/To/MyStuff”)动态更改sys.path

在“正式”Python文档中,我看到的唯一一个与前面描述的sys.path的构建相关的规则是在tutorial中的第6.1.2节
摘录:

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.

您可能还需要检查专用于Python-GdalOgrInPython绑定的GDAL Wiki,其中讨论了在Linux和Windows平台上的安装和使用。

相关问题 更多 >

    热门问题