Skip to content

Instantly share code, notes, and snippets.

@tasercake
Last active February 18, 2024 22:22
Show Gist options
  • Save tasercake/6f0973d01674c7431c9bd37e4ce3730b to your computer and use it in GitHub Desktop.
Save tasercake/6f0973d01674c7431c9bd37e4ce3730b to your computer and use it in GitHub Desktop.
Setting up pyenv virtualenvs on a Mac (with Homebrew)

Install Pyenv & Pyenv-virtualenv

Install Python build dependencies

xcode-select --install
brew install openssl readline sqlite3 xz zlib

Install using Homebrew

brew update
brew install pyenv
brew install pyenv-virtualenv

Set up your shell startup scripts

If using ZSH (default since OS X 10.15)

echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

If using Bash

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init --path)"' >> ~/.profile
echo 'if [ -n "$PS1" -a -n "$BASH_VERSION" ]; then source ~/.bashrc; fi' >> ~/.profile
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

You may need to open a new shell after this.


Use virtualenvs

Each of the below pyenv <command> operations is documented here

Install desried python version(s)

# pyenv install <python_version>
pyenv install 3.7.12

Create a virtualenv

# pyenv virtualenv <virtualenv_name> <python_version>
pyenv virtualenv myenv 3.7.12

Set a virtualenv as the default for a project/directory

This creates a .python-version file that tells pyenv which executable the python command should point to.

# To use a virtualenv:
# pyenv local <virtualenv_name>
pyenv local myenv

# To use a python base interpreter:
# pyenv local <python_version>
pyenv local 3.7.12

[Optional] Install Starship.rs

starship.rs displays useful information about your terminal environment, such as the version of Python that's currently active.

Your prompt line should then look something like this:

hypotenuse on master [$!] via 🐍 v3.7.12 (myenv) on ☁️ (us-east-1)
❯ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment