Skip to content

Instantly share code, notes, and snippets.

@lucasnad27
Last active October 31, 2021 00:40
Show Gist options
  • Save lucasnad27/b9319e8e943fa06d5e2d72dc8ab4f9d7 to your computer and use it in GitHub Desktop.
Save lucasnad27/b9319e8e943fa06d5e2d72dc8ab4f9d7 to your computer and use it in GitHub Desktop.
Setting up a python environment in Mac OSX

Purpose

An opinionated setup for python on Mac OSX. Opinions subject to change as new tooling gains traction. I've used this setup for the past ~5 years.

Prerequisites

Understanding of a .bashrc or .zshrc file. Why? There are commands that need to be ran every time you open a terminal window. If you don't have a strong opinion about your own "rc" setup file, install ohmyzsh. This will give you some sane defaults for your terminal, makes it clear where to do things like modify your $PATH env var (needed for a good python or node setup), and adds some nice extras like displaying the version of node you are currently using. The tldr; for installing ohmyzsh is below, but you'll want to read through the docs to understand how to utilize it...

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

ohmyzsh ships with a few default themes, but my all time favorite theme is spaceship. You don't need ohmyzsh to use spaceship, but it's the only way I've ever installed it. Go check out the install instructions for ohmyzsh. tldr; is below...

# clone the spaceship prompt repo into your zsh themes directory
git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt" --depth=1
# create a symlink
ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"
# set ZSH_THEME="spaceship" in your .zshrc

Don't mess with the system installed python

The OS depends on python so...yeah....don't touch it.

Use multiple python versions

pyenv enables multiple versions of python to be installed. Switching from 2.7.x to 3.8 is a breeze. Installing future versions is simple too. Read the README in its entirety and then run through the install instructions.

Isolated environments for every project

virtualenv helps manage multiple python projects with different python versions and dependencies. One could have a foo project using python 3.8 with the requests library at version 1.0. A second project, bar could use python 3.7 with the requests librarty at version 2.0. The two projects are completely isolated. Hooray!

virtualenv was built oustide the context of pyenv. So the two don't work together without a library called pyenv-virtualenv. Run through the installation instructions. tl;dr on installation below...

# clone the repo to your home directory
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
# the below echo command will append to your .zshrc file
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

Demo

In this gif, I'm checking what versions of python I have available, creating a new virtualenvironment based on 3.8.11, activating it, using pip (ok, I skipped that part), and then uninstalling the virtualenv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment