Table of Contents

Introduction

This is not meant to be a detailed guide on the many ways you could install Homebrew, and pyenv, but a look into what I did to get my environment set up for my personal projects.

Pre-requisites

Xcode Command Line Tools (link)

Homebrew

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Let’s begin.

Why pyenv?

In my personal experience, working with multiple versions of Python in Windows is as easy as downloading as many versions as you wish or need, and drop them somewhere in your C drive. For some reason that seems easier than managing multiple versions of Python in macOS.

This is where pyenv comes in.

pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.1

Before breathing new life into my MacBook Air, I had already read about pyenv in a Real Python article titled “Managing Multiple Python Versions With pyenv.”2 So following the steps from that guide, and pyenv’s own guide on using Homebrew on macOS I got my system ready for action.

Installing pyenv

Install dependencies

$ brew install openssl readline sqlite3 xz zlib

Install pyenv

$ brew install pyenv

Load pyenv automatically by running the following command to add it to your shell

Starting with pyenv v2.0.0 the pyenv init command has changed.

$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init --path)"\nfi' >> ~/.zshrc

Restart shell

$ exec "$SHELL"

Next, I’ll install specific Python versions.

Installing Python 3.9

First, let’s get a list of all 3.9 versions:

$ pyenv install --list | grep " 3\.9"
  3.9.0
  3.9-dev
  3.9.1
  3.9.2
  3.9.3
  3.9.4
  3.9.5

Installing the latest 3.9 release.

$ pyenv install 3.9.5

Installing Python 2.7

For the Ignition project I maintain, both Python 2.7.18 or Jython 2.7.1 are required.

So, first, I will install Python 2.7.18.

$ pyenv install --list | grep " 2\.7"
  2.7.0
  2.7-dev
  2.7.1
  2.7.2
  2.7.3
  2.7.4
  2.7.5
  2.7.6
  2.7.7
  2.7.8
  2.7.9
  2.7.10
  2.7.11
  2.7.12
  2.7.13
  2.7.14
  2.7.15
  2.7.16
  2.7.17
  2.7.18

Python 2.7 reached its EOL on January 1, 2020. See: Sunsetting Python 2

Installing the final 2.7 version:

$ pyenv install 2.7.18

Setting a global version of Python

Run pyenv versions to list all Python versions known to pyenv; an asterisk will be shown next to the currently active version.

$ pyenv versions
* system (set by /Users/thecesrom/.pyenv/version)
  2.7.18
  3.9.5

Run pyenv global <version> to set the global version of Python to be used in all shells.

$ pyenv global 3.9.5

Verify your selection by running pyenv versions.

$ pyenv versions
  system
  2.7.18
* 3.9.5 (set by /Users/thecesrom/.pyenv/version)

Alternatively, you could specify multiple versions as global at once.

Success!

And by doing all of the above I have completed setting up my computer for working on my projects.

Happy coding!

Further reading

  1. pyenv :: Modern Python Developer’s Toolkit - https://pycon.switowski.com/02-packages/pyenv/

Sources

  1. pyenv/pyenv: Simple Python version management - https://github.com/pyenv/pyenv 

  2. Managing Multiple Python Versions With pyenv - https://realpython.com/intro-to-pyenv/