Setting up Python3 and Pip3 on your Mac

Had a slightly confusing time doing this recently, so here’s a brief explanation of how to do this and why you might want to:

Python has several versions, 2 and the newest, 3—up to 3.7 at the time of this writing. On a Mac, your system came with one built in, probably 2.7, which is not what we want to use for installing tools such as Ansible or the AWS command line interface. So let’s leave this alone.

We want the latest 3.x version and will install it with Homebrew.

Check current Python status in the terminal (these are all terminal commands!)—you can hit tab twice after typing “python” to see all the auto-complete options of all the Python installs you may have.

python

This gave me a bunch of stuff. Not super helpful.

which -a python

This is better. Once you have the file paths, you can then check versions like:

/usr/bin/python --version

Installation with Homebrew

Make sure to define the version you want:

brew install python3

This comes with pip3, Python’s package installer/manager, which we want for Ansible, etc.

Aliases for pip and python commands

Let’s not worry about remembering to type out pip3 or python3 by aliasing them from your .bash_profile:

nano ~/.bash_profile
alias pip='pip3'
alias python='python3'

Re-open the terminal and check that this worked:

pip --version
python --version

Great 👍

Using Pip3-managed tools on the command line

You can now do things like:

pip install ansible
pip install awscli --upgrade --user

(If you don’t do the aliasing, you will want to do this:)

pip3 install ansible

Ansible will install itself to your PATH, but aws-cli lands in a Python folder, so we need to add that folder to our Bash profile. Note the version number in the file path.

nano ~/.bash_profile
export PATH="$PATH:~/Library/Python/3.7/bin"

Make sure this worked:

aws --version
aws-cli/1.16.140 Python/3.7.3 Darwin/18.2.0 botocore/1.12.130

Now you’re good to go.