2

I'm trying to instal the GDAL python package, preferably in a virtual environment, but when I try "pip install gdal", I get the following error:

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

I have GDAL version 1.11.4 installed on my Mac. I installed it via Kyngchaos, and it is located at /Library/Frameworks/GDAL.framework/Versions/1.11/Resources/gdal

the cpl_port.h file is located in /Library/Frameworks/GDAL.framework/Versions/1.11/Headers

I've read several related topics but they're all on ubuntu and using older versions of pip that have commands like "--no-install" etc.

2

I found the answer on another thread with some minor tweaks:

(Adapted from Yellowcap's answer)

Make a virtual environment:

virtualenv env
source env/bin/activate
cd env

Download the GDAL package version you want, unzip it and cd into it:

pip download GDAL==1.11.2 (or whichever version you want)
tar -zxvf GDAL-1.11.2.tar.gz
cd GDAL-1.11.2

According to Yellowcap, you might need:

export CFLAGS=-Qunused-arguments export CPPFLAGS=-Qunused-arguments

Then:

python setup.py build_ext\
     --gdal-config=/Library/Frameworks/GDAL.framework/Versions/1.11/unix/bin/gdal-config\
     --library-dirs=/Library/Frameworks/GDAL.framework/Versions/1.11/unix/lib/\
     --include-dirs=/Library/Frameworks/GDAL.framework/Versions/1.11/Headers/

Build and install:

python setup.py build
python setup.py install
1
brew install gdal

python -m venv /path/to/new/virtual/environment --system-site-packages

I found that if you have installed gdal via brew (outside of your venv) you can use the gdal python bindings from the installation within your venv as long as your venv was created using the --system-site-packages flag.

This allows your virtual environment to access modules which were installed under your system's python installation.

Two things to note:

  • It is now possible for you to accidentally use system libraries now where you may not intended to.

  • If you are trying to install a library which has a dependency on a different version of GDAL, that installation will still fail.

https://docs.brew.sh/Homebrew-and-Python#virtualenv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.