Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add GDAL as a dependency to a Python package

I am trying to package a Python script for PyPI that uses GDAL. I started by including a direct reference in my setup.py:

install_requires=['GDAL==1.11.2'],

This way the package failed to install in my test virtual environment:

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

I then tried it with a reference to pygdal, since it is marked as a virtualenv friendly version:

install_requires=['pygdal'],

This way the installation finishes without errors (but with the usual load of compilation warnings). However, when then I invoke the script I get this error back:

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

What is the correct way of setting GDAL as a dependency?

like image 453
Luís de Sousa Avatar asked Apr 07 '16 09:04

Luís de Sousa


1 Answers

After various tests I came to conclude this is an issue with the pygdal package itself. The dependency is correctly declared but pip either fails to install or compile it. I tried to install pygdal directly with pip on a stock Ubuntu 14.04 system and it fails. There is not yet a python wheel for GDAL/OGR, which might explain this issue. Please refer to this discussion for more details.

The strategy I am now employing is to simply leave the dependencies up to the user. In the source code, something like this can help the user:

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""") 

If the target system uses a package management mechanism (e.g. apt, yum) one may use it instead of PiPY to distribute GDAL dependent programmes.

like image 142
Luís de Sousa Avatar answered Oct 18 '22 08:10

Luís de Sousa