How I Python on macOS in 2020
Tags: python, macos, bash, fish
Published: 2020-10-22 01:23:52

---
title: How I Python on macOS in 2020
published: true
description: How I'm configuring Python and associated tools in late 2020.
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/4k46dm0v4pnt5j3kfo1x.png
tags: python, macos, bash, fish
---

# TL;DR

1. Use [Homebrew](https://brew.sh) for Python dependencies.
1. Use [`pyenv`](https://github.com/pyenv/pyenv) for installing Python versions.
1. Use [`virtualenvwrapper`](https://virtualenvwrapper.readthedocs.io/en/latest/) to create and manage Python virtual environments.
1. Use [`pipx`](https://pipxproject.github.io/pipx/) for Python command line tools.
1. Use [Black](https://black.readthedocs.io/en/stable/) for Python code formatting.

## Use [Homebrew](https://brew.sh) 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](https://brew.sh). Once you have Homebrew installed, you will need o make sure to have a few dependencies installed that are required for building CPython.
```shell
brew install openssl@1.1 readline
```

## Use [`pyenv`](https://github.com/pyenv/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`](https://github.com/pyenv/pyenv) for installing Python versions. If you wish, you can install `pyenv` using Homebrew.
```shell
brew install pyenv
```
I don't do this, though. I like to track the most recent changes to `pyenv`, so I use the [`pyenv-installer`](https://github.com/pyenv/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
```
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`.
```shell
pyenv global 3.8.6 3.7.9 3.9.0
```
Let's make sure everything looks good by running `pyenv versions`.
```shell
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)
```
Looks good. Let's use `pyenv which` to make sure the versions are correct.
```shell
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
```
Just the way I like it.

## Use [`virtualenvwrapper`](https://virtualenvwrapper.readthedocs.io/en/latest/) 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`](https://virtualenvwrapper.readthedocs.io/en/latest/) 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.
1. By using the `setvirtualenvproject` command, I can get easy directory switching when using the `workon` command.

## Use [`pipx`](https://pipxproject.github.io/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`](https://pipxproject.github.io/pipx/) comes in. Once installed, you have a `pipx` command which you can use to install your Python command line tools.
```shell
pipx install black
```
Let's use `pipx list` to see what happened.
```shell
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
```
`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`.
```shell
lgw4 at defiant in ~
❯ which black
/Users/lgw4/.local/bin/black
```

## Use [Black](https://black.readthedocs.io/en/stable/) for Python code formatting

I use [Black](https://black.readthedocs.io/en/stable/) 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

* Brett Cannon. [Why you should use `python -m pip`](https://snarky.ca/why-you-should-use-python-m-pip/).
* Jacob Kaplan-Moss. [My Python Development Environment, 2020 Edition](https://jacobian.org/2019/nov/11/python-environment-2020/)
* Chip Warden. [My Dotfiles](https://github.com/lgw4/dotfiles). These files show how I make all this work in Bash and Fish shells, both on macOS and GNU/Linux.