2009-10-08 220 views
10

我已經gdal安裝在Ubuntu的Jaunty運行,但我不能運行gdal2tiles,因爲我得到的錯誤:無法在python中導入gdal?

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

當我打開蟒蛇,然後鍵入import gdal我得到同樣的錯誤。

我已經set LD_LIBRARY_PATH(沒有空格!)至/usr/local/lib但它似乎沒有任何區別。

看起來像Python找不到gdal。誰能幫忙?

謝謝!

回答

14

Ith似乎是一個「Python路徑」問題。  在定義的路徑中查找Python庫。嘗試

import sys 
sys.path 

如果在gdal.py和相關文件不在此列表中的目錄,然後Python不能找到它。這是「診斷」部分。爲了解決這個問題,你有幾個選擇......他們都知道Python用來構建這條路徑的規則。

  • 的PYTHONPATH environement變量可被改變以包括期望路徑
  • Python可以從期望庫駐留在目錄啓動。
  • sys.path中可以動態改變,與sys.path.append( 「/ SomePath /要/的MyStuff」)

,我已經看到了有關SYS的建築規則的唯一地方。路徑以前描述的那樣,「官方」 Python的文檔中,是在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. 
+0

謝謝!將gdal庫添加到PYTHONPATH固定的東西(使用'export PYTHONPATH =「...」)。 – AP257 2009-10-08 16:41:59

+0

在Windows中如果沒有名爲PYTHONPATH的變量創建它併爲其添加路徑 – Roozi 2016-02-25 17:29:50

+1

如何修改PYTHONPATH環境來解決此問題?我找不到該模塊已保存在哪裏或如何編輯'PYTHONPATH'變量。 – mjp 2016-04-11 20:56:15

1

您可能還需要檢查GDAL維基致力於綁定到Python - GdalOgrInPython凡在Linux和Windows平臺上安裝和使用進行了討論。