无法在Python中导入gdal?

发布于 2024-08-06 22:40:10 字数 471 浏览 8 评论 0原文

我在 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 时> 我遇到同样的错误。

我已将 LD_LIBRARY_PATH 设置为 /usr/local/lib (不带空格!),但它似乎没有任何区别。

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-08-13 22:40:10

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

import sys
sys.path

如果gdal.py和相关文件所在的目录不在这个列表中,那么Python找不到它。这是“诊断”部分。要解决这种情况,您有多种选择......它们都取决于了解 Python 用于构建此路径的规则。

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

我在“官方”中看到了与先前描述的 sys.path 构建相关的规则的唯一地方Python 文档位于 教程 的第 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.

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.
不可一世的女人 2024-08-13 22:40:10

我也遇到了同样的问题。但我发现这可能是因为包名称与“gdal”不同。

我使用命令“conda install -c conda-forge gdal”在我的anaconda环境中安装了gdal,

然后尝试通过python脚本“import gdal”或“from osgeo import gdal”导入gdal。没用。

我查看了这个包的地址“.../anaconda3/envs/(环境名称)/lib/python3.7/site-packages”,并复制了原来的包目录,并将名称从“GDAL-3.4.7”更改为“GDAL-3.7”。 1-py3.7-linux-x86_64.egg-info”到“gdal”。
更改包名称

然后我发现它有效。我通过“import gdal”成功导入了gdal。

I met the same problem as well. But I found it might because the package name is not as same as "gdal".

I installed gdal in my anaconda environment using command "conda install -c conda-forge gdal",

then I tried importing gdal by python scripts "import gdal" or "from osgeo import gdal". Didn't work.

I look into the address of this package ".../anaconda3/envs/(environment name)/lib/python3.7/site-packages", and copied the original package directory, and changed the name from "GDAL-3.4.1-py3.7-linux-x86_64.egg-info" to "gdal".
change package name

Then I found it worked. I succeed in importing gdal by just "import gdal".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文