TransWikia.com

Install GDAL Python binding on mac

Geographic Information Systems Asked by Strernd on April 18, 2021

When I try to install gdal (on OSX 10.11.6) with pip install gdal (or easy_install) the installation fails with following error

extensions/gdal_wrap.cpp:3085:10: fatal error: 'cpl_port.h' file not found
#include "cpl_port.h"

According to this thread https://stackoverflow.com/questions/37700484/python-gdal-does-not-install-on-mac-osx-el-capitan I added the location of the header file to my path variable.
My .bash_profile now looks like this

export PATH=$PATH:/Library/Frameworks/GDAL.framework/Programs
export PATH=$PATH:/Library/Frameworks/GDAL.framework/Headers

The error remains the same.

Then I tried following this solution: Python GDAL package missing header file when installing via pip although I do not want to install it in a virtualenv.

After setting CPLUS_INCLUDE_PATH and C_INCLUDE_PATH I ran into following error:

    clang++ -bundle -undefined dynamic_lookup -Wno-error=unused-command-line-argument-hard-error-in-future build/temp.macosx-10.11-x86_64-2.7/extensions/gdal_wrap.o -L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/sqlite/lib -L/Library/Frameworks/GDAL.framework/Versions/2.1/lib -lgdal -o build/lib.macosx-10.11-x86_64-2.7/osgeo/_gdal.so
ld: warning: directory not found for option '-L/Library/Frameworks/GDAL.framework/Versions/2.1/lib'
ld: library not found for -lgdal
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang++' failed with exit status 1

Any idea how to fix the errors and get a working installation?

7 Answers

The osgeo module is in

enter image description here

Therefore the content of the gdal-py2.7.pth should be

import sys; sys.path.insert(0,'/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages')

Therefore:

from osgeo import gdal
gdal.__file__
'/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages/osgeo/gdal.pyc'
# or 
import gdal
gdal.__file__
'/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages/osgeo/gdal.pyc'

Answered by gene on April 18, 2021

As of today (September 2017), the GDAL package only provides built-in support for python 2.7. Here is how you can install it for python 3+:

  1. Find the exact version of GDAL you are using:
$ ls /Library/Frameworks/GDAL.framework/Versions
2.1     Current
  1. Download (not install!) the GDAL package using pip (>= 9.0.01) — it is important to download the exact same version of the python GDAL package as the one you have installed, otherwise this will not work:
$ cd /tmp 
$ pip3 download GDAL==2.1
  1. Decompress the downloaded archive and go into the corresponding folder
$ tar xzf GDAL-2.1.0.tar.gz
$ cd GDAL-2.1.0
  1. Install GDAL:
$ python3 setup.py install 
    --include-dirs=/Library/Frameworks/GDAL.framework/Versions/2.1/unix/include 
    --library-dirs=/Library/Frameworks/GDAL.framework/Versions/2.1/unix/lib

If python3 is your default, replace pip3 and python3 by pip and python. Do not forget to replace 2.1.0 and 2.1 in the above commands by your GDAL version.

Once you are done, do not test the python installation in the GDAL installation directory, you would get horrible errors. Go somewhere else and try to import a GDAL package:

$ cd /tmp
$ python3
>>> import osgeo
>>> osgeo.version_info
sys.version_info(major=3, minor=5, micro=4, releaselevel='final', serial=0)

1 If you are using pip < 9.0.0, you can see this answer for a way to replace the given commands.

Answered by Holt on April 18, 2021

Use Anaconda: https://www.anaconda.com

Open up Terminal: conda install gdal

Answered by Reid Falconer on April 18, 2021

You received the error because the header file was not included in your GDAL build. In order to get all of the required headers, you need to use the latest development branch of GDAL, as it added another header ~3 weeks ago. Set your CC and CXX environmental variables to clang and clang++, then install using Homebrew:

brew install gdal --HEAD

If you try to build GDAL with gcc-8 instead, it will throw syntax errors. As of June 2018 this provides the development branch of GDAL 2.3.0. To install gdal for your existing Python (e.g., Anaconda Python 3.6) and link to GDAL, follow the method of @Holt, but with the below modifications.

$ cd /private/tmp 
$ pip download GDAL==2.3
$ tar xzf GDAL-2.3.0.tar.gz
$ cd GDAL-2.3.0

Now, set CC and CXX to gcc-8 and g++-8. Next, install gcc-8 with Homebrew if not currently installed: brew install gcc. Then, make the following change to the setup.cfg file:

gdal_config=/usr/local/bin/gdal-config

gdal-config contains all of the information needed to link to include and library files. Next, make the following modifications to the setup.py file:

cxx11_flag='-std=c++11 -stdlib=libc++'
os.environ['ARCHFLAGS'] = ''
name='gdal'

This sets ARCHFLAGS to blank rather than '-Wunused-command-line-argument-hard-error-in-future', as the latter is incompatible with gcc. Setting stdlib should help clang find the correct C++11 libraries, if used. Last, changing the name from GDAL to gdal lists the package name correctly under conda list. Finally, install the Python package correctly linked to GDAL 2.3.0:

python setup.py install

As @Holt noted, be sure to load and test the install in another folder.

Rant: It's crazy that this much effort is needed to install gdal for Anaconda Python 3.6 linked to a centralized GDAL 2.3.0 installation, but that is because the conda-forge gdal folks intentionally broke the connection to Homebrew GDAL ~2 years ago in an effort to equally [not] support MacPorts. This is nuts in my opinion, as others also build from source and managing multiple GDAL installations is a pain. The GDAL package for R, rgdal, correctly links to Homebrew GDAL during installation without any hassle. The GDAL library for Anaconda Python should also do this!

Answered by Adam Erickson on April 18, 2021

If you landed here trying to install geopandas, what worked for me on a 10.13 mac is:

python3.7 -m pip install cython
python3.7 -m pip install git+https://github.com/jswhit/pyproj.git

brew went ahead and tried to install a whole slew of dependencies, while what I needed was just pyproj to install geopandas.

Answered by Mr_and_Mrs_D on April 18, 2021

I made a tutorial https://github.com/felipunky/GISPython scroll down to the Mac section.

An extract:

1.) Download and install python 2.7.

2.) Go to terminal, you can do so by pressing at the same time the Command and Space keys, than type terminal.

3.) Go to http://www.kyngchaos.com/software/frameworks/ and download and install the GDAL 1.11 version.

4.) Now open the file explorer and press the Command, Shift and G key at the same time, this should enable you to search a directory. Type /Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages and press enter. We need these files to be on our python site-packages folders. So copy all the files and folders.

5.) Repeat the Command, Shift and G press to go to another directory, this time is our python's directory which is at: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ once you are inside the directory paste all the files you have already copied from step 4.).

6.) Check if we have succesfully installed the library: go to Terminal and type: python than press enter, now type: import gdal if there are no errors we can continue with step 7.).

7.) Install homebrew: Here is a detailed guide on how to install homebrew: http://osxdaily.com/2018/03/07/how-install-homebrew-mac-os/

8.) Go to Terminal and type:

brew install matplotlib

brew install numpy

brew install geos

brew install proj

9.) Test by typing python on Terminal to access the python console. Then type:

import gdal

If there are no errors you are good to go.

Answered by Felipe Gutierrez on April 18, 2021

Try this:

brew install gdal
brew upgrade gdal
pip install gdal

Answered by azis511 on April 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP