且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在python中导入gdal?

更新时间:2022-03-02 10:20:24

Ith似乎是" Python路径 "问题.在定义的路径中查找Python库.试试

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

import sys
sys.path

如果gdal.py和相关文件所在的目录不在此列表中,则Python无法找到它.这是针对诊断"部分的.要解决这种情况,您有几种选择...它们全部取决于了解Python用于构建此路径的规则.

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.

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

在官方" Python文档中,唯一可以看到以前描述的有关sys.path的构建规则的地方是

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.