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

6

我正在尝试为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'], 这样安装就不会出现错误(但是通常会有大量编译警告)。然而,当我调用脚本时,我会收到以下错误信息:
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作为依赖项?

1个回答

1
经过各种测试,我得出结论,这是与pygdal包本身有关的问题。依赖项已正确声明,但pip要么无法安装,要么无法编译它。我尝试在原始的Ubuntu 14.04系统上直接使用pip安装pygdal,结果失败。GDAL/OGR还没有Python wheel,这可能解释了这个问题。请参阅此讨论以了解更多详细信息。
我现在采用的策略是简单地将依赖项留给用户处理。在源代码中,像这样的内容可以帮助用户:
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依赖程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接