Luca Grippa

Setting up Pyenv in .zshrc

~/.zshrc

# Activate Homebrew in the current shell session
eval "$(/opt/homebrew/bin/brew shellenv)"
 
# Set the PYENV_ROOT variable to point to the location of Pyenv
export PYENV_ROOT="$HOME/.pyenv"
 
# Check if pyenv command is available, if not, add Pyenv binary directory to PATH
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
 
# Initialize pyenv into the shell session
eval "$(pyenv init -)"
 
# Initialize pyenv-virtualenv plugin into the shell session
eval "$(pyenv virtualenv-init -)"

Setting up pyenv

  1. Install pyenv with Homebrew
brew update
brew install pyenv
  1. Define environment variable PYENV_ROOT to point to the path where pyenv will store its data. $HOME/.pyenvis the default.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
  1. Add the pyenv executable to your PATH if it's not already there
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
  1. Run eval "$(pyenv init -)" to install pyenv into your shell as a shell function, enable shims and autocompletion
    • You may run eval "$(pyenv init --path)" instead to just enable shims, without shell integration
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

Setting up pyevn-virtualenv

  1. Install pyenv-virtualenv via Homebrew
$ brew install pyenv-virtualenv
  1. Activate pyenv-virtualenv in the shell environment by appending the initialization script to the .zshrc file:
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

References

  1. https://github.com/pyenv/pyenv?tab=readme-ov-file#getting-pyenv
  2. https://github.com/pyenv/pyenv?tab=readme-ov-file#set-up-your-shell-environment-for-pyenv
  3. https://github.com/pyenv/pyenv-virtualenv?tab=readme-ov-file#installing-with-homebrew-for-macos-users