如何将 GDAL 添加为 Python 包的依赖项

How to add GDAL as a dependency to a Python package

我正在尝试为使用 GDAL 的 PyPI 打包 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'],

这样安装就没有错误地完成了(但通常会出现编译警告)。但是,当我调用脚本时,我得到了这个错误:

Traceback (most recent call last):
  File "/home/desouslu/.virtualenvs/test_p3/bin/hasc2gml", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 2716, in <module>
    working_set.require(__requires__)
  File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 685, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 588, in resolve
    raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: pygdal

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

经过各种测试后,我得出结论,这是 pygdal 包本身的问题。依赖项已正确声明,但 pip 无法安装或编译它。我尝试在库存 Ubuntu 14.04 系统上直接使用 pip 安装 pygdal,并尝试 it fails. There is not yet a python wheel for GDAL/OGR, which might explain this issue. Please refer to 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 依赖程序。