loading...
Cover image for How I Python on macOS in 2020

How I Python on macOS in 2020

lgw4 profile image Chip Warden ・4 min read

TL;DR

  1. Use Homebrew for Python dependencies.
  2. Use pyenv for installing Python versions.
  3. Use virtualenvwrapper to create and manage Python virtual environments.
  4. Use pipx for Python command line tools.
  5. Use Black for Python code formatting.

Use Homebrew for Python dependencies

Most developers using macOS are probably already using Homebrew for installing tools and libraries not included with the default system. If you are not, you can find instructions for installing Homebrew here. Once you have Homebrew installed, you will need o make sure to have a few dependencies installed that are required for building CPython.

brew install openssl@1.1 readline
Enter fullscreen mode Exit fullscreen mode

Use pyenv for installing Python versions

Apple does not track upstream Python releases very closely, so you should install your own Python versions so you can stay up to date. I recommend using pyenv for installing Python versions. If you wish, you can install pyenv using Homebrew.

brew install pyenv
Enter fullscreen mode Exit fullscreen mode

I don't do this, though. I like to track the most recent changes to pyenv, so I use the pyenv-installer to install pyenv. Whichever method you use, make sure to follow the installation instructions for your shell and run pyenv doctor to make sure everything is working well.

Install some Python versions and set the global Python versions

I install the most recent Python 3.7 (to support the iTerm shell integration utilities), Python 3.8 (which I make the default global python3), and Python 3.9.

pyenv install 3.7.9
pyenv install 3.8.6
pyenv install 3.9.0
Enter fullscreen mode Exit fullscreen mode

Next, I like to make Python 3.8 my default global python3 (and python3.8) executable, but I also like to have python3.7 and python3.9 available on my PATH.

pyenv global 3.8.6 3.7.9 3.9.0
Enter fullscreen mode Exit fullscreen mode

Let's make sure everything looks good by running pyenv versions.

lgw4 at defiant in ~
❯ pyenv versions
  system
* 3.7.9 (set by /Users/lgw4/.pyenv/version)
* 3.8.6 (set by /Users/lgw4/.pyenv/version)
* 3.9.0 (set by /Users/lgw4/.pyenv/version)
Enter fullscreen mode Exit fullscreen mode

Looks good. Let's use pyenv which to make sure the versions are correct.

lgw4 at defiant in ~
❯ pyenv which python3
/Users/lgw4/.pyenv/versions/3.8.6/bin/python3

lgw4 at defiant in ~
❯ pyenv which python3.8
/Users/lgw4/.pyenv/versions/3.8.6/bin/python3.8

lgw4 at defiant in ~
❯ pyenv which python3.9
/Users/lgw4/.pyenv/versions/3.9.0/bin/python3.9

lgw4 at defiant in ~
❯ pyenv which python3.7
/Users/lgw4/.pyenv/versions/3.7.9/bin/python3.7
Enter fullscreen mode Exit fullscreen mode

Just the way I like it.

Use virtualenvwrapper to create and manage Python virtual environments

You are using virtual environments with Python, right? These days, Python comes with a perfectly fine virtual environment implementation built in, which you can use by running python3 -m venv venv. However, I like to use virtualenvwrapper to manage my virtual environments for two reasons:

  1. I sometimes forget to add venv to my .gitignore file and might accidentally commit my virtual environment to my Git repository.
  2. By using the setvirtualenvproject command, I can get easy directory switching when using the workon command.

Use pipx for Python command line tools

If you are like me, you have a few Python command line tools that you always want available, but you don't want to install them in every virtual environment you create. That's where pipx comes in. Once installed, you have a pipx command which you can use to install your Python command line tools.

pipx install black
Enter fullscreen mode Exit fullscreen mode

Let's use pipx list to see what happened.

lgw4 at defiant in ~
❯ pipx list
venvs are in /Users/lgw4/.local/pipx/venvs
apps are exposed on your $PATH at /Users/lgw4/.local/bin
   package black 20.8b1, Python 3.8.6
    - black
    - black-primer
    - blackd
Enter fullscreen mode Exit fullscreen mode

pipx creates a private virtual environment for your command line tool, then makes a symbolic link for the tools in ~/.local/bin so they are available on your PATH.

lgw4 at defiant in ~
❯ which black
/Users/lgw4/.local/bin/black
Enter fullscreen mode Exit fullscreen mode

Use Black for Python code formatting

I use Black to format all my Python code. Is it a perfect tool that formats everything they way I would manually? No. But it does a good job, is close enough to the PEP8 standard to not bother me, and works fast. It is also a PSF project, so I feel confident that it will be around and maintained for a long time.

References

Discussion

pic
Editor guide