1

This question has been put on hold at the GIS Stack Exchange for being off-topic. So I thought I'd try StackOverflow instead.

I have been using Python for a little while and have bought into the idea that virtual environments are a good thing because they prevent future updates of packages breaking your precious code that has taken eons to develop. I have just spent a good few days trying to get cartopy working in a Python 3.6 virtual environment on a Mac using fink-installed libraries for gdal, geos and proj4 (see Python 3.4 crashes when producing some – but not all – Cartopy maps with segmentation fault 11). However, it seems that, even in the virtual environment, the code still relies on those external libraries. This would seem to defeat the object of using virtual environments because, if the external libraries are updated, it runs the risk of breaking any GIS code developed in the virtual environment, even if the Python packages in the virtual environment remain unchanged. The only time I've come across this situation is in relation to GIS-related packages (but, clearly, it may occur in other spheres as well).

Is it possible to create a Python virtual environment for GIS that may be built initially using external libraries and software but is then self-contained and is unaffected when external libraries and frameworks are changed or updated?

Community
  • 1
  • 1
user1718097
  • 4,090
  • 11
  • 48
  • 63

1 Answers1

2

Python virtual environments are great, but as you're discovering don't really help when you're trying to isolate C based dependencies.

Docker is one solution, but might be overkill for your situation. Conda is an environment manager which solves this problem by taking care of your Python packages and all of their dependencies, regardless of what language they were written in. Cartopy and many other common GIS packages are available pre-built through the conda-forge repository.

Once you have Conda installed you can just do:

conda create -n my_cartopy_env -c conda-forge cartopy

And cartopy and all it's dependencies should be installed for you in their own environment, and it works on Windows, OS X or Linux. Magic :-)

Duncan WP
  • 830
  • 5
  • 19
  • +1 for Conda, works great. But some care should still be taken. Not all conda+gdal builds set the system environments correctly when activating a new env. Recent Conda versions + GDAL from conda-forge do it well these days (by means of running `gdal-activate.bat`). But previous versions for example didn't overwrite the sys variable if it was already set (eg manually), which left you still using different binaries than the ones you would expect. Its package/build dependent, so no guarantees. Explore the sys variables before and after activating environments to be sure. – Rutger Kassies May 10 '17 at 10:38
  • +1 for rephrasing my original question succinctly using correct terminology - at least I now know what I'm trying to do. Conda is a good suggestion and I will check it out. I've upvoted the question but I won't mark it as the 'accepted answer' yet in case anyone else posts another solution. – user1718097 May 12 '17 at 09:33