Setting up a usable Python environment in macOS

So having recently gone back to the Mac after a 7 year hiatus to try out using Linux as a desktop environment meant getting 7 years worth of code working. Any Perl code I had would have been originally written on macOS, so it worked just fine. 3/4 of my C code was written on macOS originally as well. But Just installing some packages with Brew and it all compiled just fine. And of course, all the BASH and awk code was no issues either.

And that brings us to Python. For some inexplicable reason, the system python is still 2.7. As you’d guess, all my code is Python 3. But, you should never use the system python anyway.

There are various tutorials on getting Python on macOS working, but I ran into a few issues, so I’d thought I’d bring it all together in one place. My main use for Python is number crunching, so I have a few specific Python packages I need. And getting some working was much harder than it probably should have been.

To get started, first you’ll need a working brew installation. You can get that from https://brew.sh. With that, you’ll want to install pyenv. It’s this nice tool that lets you manage python environments.

NOTE: None of the commands require any sort of privilege escalation. However, depending on your security settings, you may be asked if terminal can access certain resources. For this to work, always authorize such requests.

So the first step after installing brew, if it’s not already installed is to install pyenv:

# brew install pyenv

Once that’s done, you can get a list of the available python versions:

# pyenv install -l
...
  3.8.2
  3.8.3
  3.8.4
  3.8.5
  3.8.6
  3.9.0
  3.9-dev
  3.10-dev
  activepython-2.7.14
  activepython-3.5.4
...

It’s an exhaustive list. But I selected 3.9.0 and set it as my global version:

# pyenv install 3.9.0
# pyenv global 3.9.0
# pyenv versions
  system
* 3.9.0 (set by /Users/dwalker/.pyenv/version)

As you can see from the output of pyenv versions above, it installed and is configured.

Next is to configure zsh to use the pyenv installation. To do this, add the following to your ~/.zshrc:

# set up pyenv 
if command -v pyenv 1>/dev/null 2>&1
then
   eval "$(pyenv init -)"
fi

Then launch a new shell and verify:

# which python 
/Users/dwalker/.pyenv/shims/python

Now for any modules or libraries, you’ll want to use pip to install them. The first thing you’ll need is wheel:

# pip install wheel

Once that is installed, then you can install other things you need. I needed numpy, matplotlib, and pygame. While trying to install them, I got a nice variety of errors. But, eventually, I got them installed with these steps:

# brew install openblas sdl sdl_image sdl_mixer sdl_ttf portmidi
# OPENBLAS="$(brew --prefix openblas)" pip install numpy
# pip install matplotlib

Then there is an issue with pygame and Python 3.9, so I had to install 2.0 from the dev branch:

pip install pygame --pre

And then to verify everything I need is installed:

#  pip list
Package         Version
--------------- -----------
certifi         2020.6.20
cycler          0.10.0
kiwisolver      1.2.0
matplotlib      3.3.2
numpy           1.19.2
Pillow          8.0.0
pip             20.2.3
pygame          2.0.0.dev14
pyparsing       2.4.7
python-dateutil 2.8.1
setuptools      49.2.1
six             1.15.0
wheel           0.35.1

And the final thing I had to do was, some older python scripts pointed to /usr/bin/python3 in their shebang lines. All of them I moved to use:

#!env python

And so far, all my code seems to be working.

Leave a Reply