Developing with Multiple Python Versions on Mac OS X

Matt Majewski
Inside the Embassy
Published in
3 min readSep 17, 2019

As a developer begins working on multiple services in a service-oriented/microservice architecture, it may be required to use multiple versions of the same language when working on new and legacy apps. It’s important to have separate virtual environments to help avoid issues with local packages and version differences.

When it comes to python, there are a few options to make life easier when switching between different virtual environments with separate versions of python.

Here I will explore a few ways to manage local python environments focused on MacOS. The two major tools I’ve used are Conda and PyEnv.

Quick Tips

Check your version of python:

python --version

Check the path of your current python:

which python

Conda

There are a few different builds of Conda. For this example, Python 3 Miniconda was chosen.

Installation Guide: https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html

Miniconda installer page: https://conda.io/miniconda.html

Note that you can download a docker image if you prefer.

To get Miniconda up and running, follow these steps:

  • Download the latest Bash Installer for MacOS
  • Verify the installation file using MD5, go to https://repo.continuum.io/miniconda/ for the MD5 sums, and run a command, ex. `md5 Miniconda3-latest-MacOSX-x86_64.sh` to verify the sum matches.

Commands:

List your environments:

conda info --envs

To create a new environment, use the following command where -n is the name:

conda create --name python2env python=2.7.14

To activate/deactivate the environment:

source activate python2env
source deactivate

To get info about the current environment:

conda info

For more advanced settings, you can reference the help / docs:

conda help

There’s also a helpful cheat sheet here: https://conda.io/docs/_downloads/conda-cheatsheet.pdf

pyenv

For MacOS, I chose to install with Homebrew.

brew update
brew install pyenv

At this point, you need dependencies installed to be able to install new versions of python. See https://github.com/pyenv/pyenv/wiki#suggested-build-environment This depends on what you need, and what you already have installed. In my case, I already had xcode command line tools installed, but to install it, run the following command:

xcode-select --install

I needed zlib as well. You may need other dependencies, if you have problems you can reference Common Build Problems. All of the issues I encountered were solved by one of the answers there.

brew install zlib

Try to install a version. For a list of versions,

pyenv install -l 

Install a version using:

pyenv install 3.7.0

In my case, I had a zlib issue, and had to use:

CPPFLAGS="-I$(brew --prefix zlib)/include" pyenv install -v 3.7.0

Add the following to .bash_profile to load pyenv automatically:

eval "$(pyenv init -)"

For a list of versions installed, run:

pyenv versions

After installing the versions you’d like, you can activate a version in a single terminal session by running:

pyenv shell 2.7.14

Having the ability to easily switch versions as you are working on different projects speeds up development and improves quality of life. Developing using the same version as production gives piece of mind and allows to debug faster as well as avoiding issues stemming from environment differences across teams.

Do you have any other tools or strategies for managing multiple environments? We’d love to see feedback in the responses.

Sources: https://github.com/pyenv/pyenv, https://conda.io/

Photo by Ben Kolde on Unsplash

If you want to see what else our engineering team is working on, come join us at Ambassador!

--

--