13

I am trying to setup an isolated python virtualenv to work on GIS projects on my Mac OS X. Sounds like I will need to install the GEOS, PROJ, GDAL/OGR from the kyngchaos site here http://www.kyngchaos.com/software/frameworks and not easy_install it into my virtualenv.

The downloads available on kyngchaos are pkg installers and not just an egg or bdist of python modules. So, I am not very sure as to what else the installer is doing to the environment other than just copying files to /Library/Framework/geos.framework folder.

How would I go about installing a specific version of GEOS, PROJ, GDAL/OGR into my new python virtualenv?

8 Answers 8

5

If you need to use OSX you can easily avoid installation complexities by installing homebrew!

After this the only commands you will need to enter are:

brew install gdal

It will automatically install also proj and geos because they are gdal dependencies.

2
  • 3
    I don't believe this addresses the issue of accessing gdal from within a virtualenv (I tried it and it did not work)
    – djq
    Commented Oct 11, 2014 at 15:37
  • 1
    you can use brew install gdal as long as you create your venv with the --system-site-packages flag, see my answer here gis.stackexchange.com/a/345248/155269
    – 88jayto
    Commented Dec 17, 2019 at 19:53
4

If each Framework of Kyngchaos is in the PATH, it is easy. Every framework has a Unix folder, with the libraries for compiling things like pyproj or geos python wherever you want, even in a private Python environment (I've never had a problem)

The only problem is that Gdal python is in the Gdal Framework. But in site-packages there a file gdal.pth

gdal.pth file:

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

if you do not want to compile it yourself, copy this file in your isolated python virtualenv

4
  • having installed those projects by hand, through 3rd party installers (fink/macports/whatever) and through other means I will guarantee to you that the best way is through Kynchaos! +1000 Commented Feb 24, 2012 at 1:39
  • @Ragi Yaser Burhum – I'm beginning to agree with you! I have been trying to use Fink to install necessary dependencies to run Cartopy in Python. I'm getting: "OSError: Could not find lib geos_c or load any of its variants" even though libgeos_c.dylib is at /sw/opt/libgeos3.6.1/lib and I've added that path to .bash_profile. I don't suppose you have any pearls of wisdom based on your experiences with fink? Commented May 4, 2017 at 0:18
  • @use use Homebrew :) Commented Jun 1, 2017 at 20:52
  • @RagiYaserBurhum ;-) Commented Jun 2, 2017 at 22:07
4

I make replicable Python/C/C++ GIS environments using virtualenv and zc.buildout. My ichpage https://github.com/sgillies/ichpage project is a little dated but could serve as a template for an up-to-date one (like the one I use for my Pleiades site development work and deployments). Blog post about it at http://sgillies.net/blog/856/i-can-has-python-and-gis-environments/.

3

Virtualenv allows you to provision a private Python environment, but does not extend to system libraries outside of the Python universe. The three packages you mentioned are all C/C++ applications at their core, so while they have interfaces for use with Python, they cannot be packaged without interacting with the C libraries (libc on up).

Because of the difficulty installing those packages from scratch, particularly on OS X, you may want to think about running a sandboxed virtual machine using something like VirtualBox and install the packages through apt on that sandboxed machine: while it is possible to do the same on OS X, it probably isn't worth the effort.

1
  • This is a good recommendation. I followed this approach using Fusion, not VirtualBox, but I might try Virtual Box next time. I used CentOS for the OS in the box. (Not the best OS choice due to the stable nature and older versions of Python, etc.)
    – DavidF
    Commented Feb 22, 2011 at 16:55
1

I found a good blog post that provides another solution that worked in my case:

http://linfiniti.com/2013/02/installing-python-gdal-into-a-python-virtualenv-in-osx/

The only difference to the blog post is that I had to do is to download the correct version of GDAL that matched the kyngchaos packages. In my case I was using the GDAL framework version 1.10, so the following install was successful

source venv/bin/activate
pip install --no-install GDAL==1.10.0
cd venv/build/GDAL
python setup.py build_ext\
    --gdal-config=/Library/Frameworks/GDAL.framework/Versions/1.10/unix/bin/gdal-config\
    --library-dirs=/Library/Frameworks/GDAL.framework/Versions/1.10/unix/lib/\
    --include-dirs=/Library/Frameworks/GDAL.framework/Versions/1.10/Headers/

Then leave the GDAL directory and install without download

cd ~
pip install --no-download GDAL

UPDATE: You might have to add some env vars before calling setup.py for the compilation step to work (see this question)

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

Yellowcap's answer above worked for me with some minor tweaks:

pip install --no-install is no longer an option. Instead, inside the env directory:

pip download GDAL==1.11.2 (or whichever version you want)

Unzip the downloaded directory and cd into it:

tar -zxvf GDAL-1.11.2.tar.gz
cd GDAL-1.11.2

pip install --no-download is also no longer an option. Instead stay in the GDAL directory and run:

python setup.py build
python setup.py install
0

NextGIS installer have isolated gdal, geos, proj, etc. and python bindings (Python 2.7) to gdal. Installation not affected on system libraries (only several environment variables in bash_profile).

0

After 9 years, it's still an issue to install GDAL and co. As said, if you don't go with conda but brew, you cannot access GDAL from the virtualenv. You then have 3 options:

  1. Create a virtualenv with the flag --system-site-packages,
  2. Use pygdal,
  3. Link the recipe from brew.

Here is how to do it:

brew tap osgeo/osgeo4mac
brew install osgeo-gdal
brew install osgeo-gdal-python

# link gdal
brew link osgeo-gdal --force
brew link osgeo-gdal-python --force

# create venv
python3 -m venv venv && . venv/bin/activate && pip install --upgrade pip setuptools 

# install gdal
pip install gdal

Careful, about your PATH. You may have GDAL with a Postgres installation, and this will conflict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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