如何将GDAL作为依赖项添加到Python包中

2024-06-16 16:35:14 发布

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

我正在尝试为PyPI打包一个使用GDAL的Python脚本。我首先在我的setup.py中包含一个直接引用:

install_requires=['GDAL==1.11.2'],

这样,包无法在我的测试虚拟环境中安装:

extensions/gdal_wrap.cpp:2855:22: fatal error: cpl_port.h: No such file or directory
 #include "cpl_port.h"
                      ^
compilation terminated.
error: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

然后我尝试引用pygdal,因为它被标记为virtualenv友好版本:

install_requires=['pygdal'],

这样安装就完成了,不会出现错误(但通常会加载编译警告)。但是,当我调用脚本时,我会返回此错误:

^{pr2}$

将GDAL设置为依赖项的正确方法是什么?在


Tags: installpy脚本pypiport错误withsetup
1条回答
网友
1楼 · 发布于 2024-06-16 16:35:14

经过各种测试后,我得出结论,这是pygdal包本身的问题。已正确声明依赖项,但pip无法安装或编译它。我试图直接用pip在一个现有的ubuntu14.04系统和it fails直接安装pygdal。目前还没有一个用于GDAL/OGR的python wheel,它可以解释这个问题。有关详细信息,请参阅this discussion。在

我现在采用的策略是将依赖关系留给用户。在源代码中,这样做可以帮助用户:

try:
    from osgeo import ogr
except ImportError:
    raise (""" ERROR: Could not find the GDAL/OGR Python library bindings. 
               On Debian based systems you can install it with this command:
               apt install python-gdal""") 

如果目标系统使用包管理机制(例如aptyum),那么可以用它代替PiPY来分发依赖GDAL的程序。在

相关问题 更多 >