Skip to content

Instantly share code, notes, and snippets.

@tasercake
Last active March 11, 2025 13:59
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:

tasercake on master [$!] via 🐍 v3.7.12 (myenv) on ☁️ (us-east-1)
❯ 
@xibolun
Copy link

xibolun commented Mar 11, 2025

there is a spot :)
I use pyenv-virtualenv with 1.2.4 version. the command of create virtualenv is pyenv virtualenv 3.7.12 myenv .
may be the command is different at different version.

brew info pyenv-virtualenv
==> pyenv-virtualenv: stable 1.2.4 (bottled), HEAD
pyenv virtualenv --help
Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>

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