Published on

Installing and Using pyenv on macOS

Authors
  • avatar
    Name
    Winston Brown
    Twitter

Installing and Using pyenv on macOS

If you're a Python developer, you know that managing different Python versions for various projects can be a hassle. pyenv is a fantastic tool that simplifies this process, allowing you to switch between multiple Python versions seamlessly. This guide will walk you through installing and using pyenv on your Mac.

Why use pyenv?

pyenv lets you install multiple Python versions side-by-side and choose which version to use for a specific project. This helps in avoiding dependency conflicts and ensures that your projects run on the correct Python version.

Installation with Homebrew

The easiest way to install pyenv on macOS is by using Homebrew.

  1. Install pyenv:

    brew update
    brew install pyenv
    
  2. Configure your shell:

    You need to add pyenv to your shell's configuration file. You can copy and paste the following commands directly into your terminal.

    • For Zsh (default on recent macOS versions):

      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
      echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
      echo 'eval "$(pyenv init -)"' >> ~/.zshrc
      
    • For Bash:

      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
      echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
      echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
      
  3. Restart your shell:

    For the changes to take effect, restart your terminal or run:

    exec "$SHELL"
    

Basic Usage

Here are some of the most common pyenv commands:

  • List available Python versions to install:

    pyenv install --list
    
  • Install a specific Python version:

    pyenv install 3.10.4
    
  • List all installed Python versions:

    pyenv versions
    
  • Set the global Python version:

    This version will be used by default in your system.

    pyenv global 3.10.4
    
  • Set a local (project-specific) Python version:

    This will create a .python-version file in your current directory, and pyenv will automatically use the specified version when you are in this directory.

    pyenv local 3.9.12
    
  • Set a shell-specific Python version:

    This version will only be used for the current shell session.

    pyenv shell 3.8.13
    

Managing Virtual Environments with pyenv-virtualenv

While pyenv manages Python versions, the pyenv-virtualenv plugin helps you manage virtual environments for your projects.

  1. Install pyenv-virtualenv:

    brew install pyenv-virtualenv
    
  2. Configure your shell for pyenv-virtualenv:

    Add the following line to your shell configuration file (.zshrc or .bash_profile):

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

    Remember to restart your shell after adding this line.

  3. Create a virtual environment:

    pyenv virtualenv <python_version> <environment_name>
    

    For example:

    pyenv virtualenv 3.10.4 my-project-env
    
  4. Activate and deactivate your environment:

    pyenv activate <environment_name>
    pyenv deactivate
    

    You can also set a local virtual environment for a project, which will be activated automatically when you enter the directory:

    pyenv local my-project-env
    

With pyenv and pyenv-virtualenv, you can now manage your Python versions and virtual environments with ease. Happy coding!