- Use Homebrew for Python dependencies.
pyenvfor installing Python versions.
virtualenvwrapperto create and manage Python virtual environments.
pipxfor Python command line tools.
- 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 email@example.com readline
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
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.
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.8) executable, but I also like to have
python3.9 available on my
pyenv global 3.8.6 3.7.9 3.9.0
Let's make sure everything looks good by running
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.
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.
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:
- I sometimes forget to add
.gitignorefile and might accidentally commit my virtual environment to my Git repository.
- By using the
setvirtualenvprojectcommand, I can get easy directory switching when using the
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
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
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
lgw4 at defiant in ~ ❯ which black /Users/lgw4/.local/bin/black
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.