Skip to content

Pyenv Virtual Environments Setup Guide

1. Installing Pyenv

More detailed instruction for installation can be found here for Mac/Linux and here for Windows.

Mac Installation

  1. Install Homebrew (if not already installed):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

  2. Install Pyenv using Homebrew:

    brew update
    brew install pyenv
    

  3. Configure the shell to use Pyenv:

  4. For Zsh (default on macOS):
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
    source ~/.zshrc
    
  5. For Bash:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
    source ~/.bashrc
    

  6. Verify Installation:

    pyenv --version
    

Linux Installation

  1. Install Dependencies (Required for Pyenv to work properly):

On Debian/Ubuntu-based systems:

sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git

  1. Install Pyenv:

    curl https://pyenv.run | bash
    

  2. Configure the shell to use Pyenv:

  3. For Bash:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
    source ~/.bashrc
    

  4. Verify Installation:

    pyenv --version
    

Windows Installation (Using Pyenv-Win)

  1. Install Pyenv-Win

    git clone https://github.com/pyenv-win/pyenv-win.git $env:USERPROFILE\.pyenv
    

  2. Add Pyenv to the system PATH: Open PowerShell and run:

    [System.Environment]::SetEnvironmentVariable("PYENV", "$env:USERPROFILE\.pyenv", "User")
    [System.Environment]::SetEnvironmentVariable("PYENV_ROOT", "$env:PYENV", "User")
    [System.Environment]::SetEnvironmentVariable("Path", "$env:PYENV\bin;$env:PYENV\shims;" + $env:Path, "User")
    

  3. Restart PowerShell and Verify Installation:

    pyenv --version
    


2. Setting Up Virtual Environments in Pyenv

Installing a Specific Python Version

To install a specific version of Python using Pyenv, run:

pyenv install 3.9.7
Replace 3.9.7 with the desired Python version.

Listing Available Python Versions

To see installed Python versions:

pyenv versions

To see all available Python versions:

pyenv install --list


3. Activating and Deactivating Virtual Environments

Creating a Virtual Environment

To create a new virtual environment:

pyenv virtualenv 3.9.7 myenv

Activating a Virtual Environment

To activate the environment:

pyenv activate myenv

Deactivating a Virtual Environment

To deactivate an environment:

pyenv deactivate


4. Managing Packages Within a Virtual Environment

Installing Packages

Once inside an environment, install packages using:

pip install numpy pandas matplotlib

Listing Installed Packages

To list all installed packages:

pip list

Updating Packages

To update all packages:

pip install --upgrade pip
pip list --outdated | awk '{print $1}' | xargs pip install --upgrade

Removing a Package

To remove a package:

pip uninstall package-name


5. Deleting Virtual Environments

To delete an environment:

pyenv virtualenv-delete myenv

Force Deleting an Environment (Manually)

If needed, manually remove an environment:

rm -rf ~/.pyenv/versions/myenv
(For Linux/Mac users, adjust the path accordingly.)

For Windows:

Remove-Item -Recurse -Force $env:PYENV\.pyenv\versions\myenv


Final Notes

  • Always activate the correct virtual environment before running scripts.
  • Use pip install for package management within environments.
  • Regularly update Pyenv with:
    pyenv update
    

By following this guide, you’ll efficiently manage Python environments across Windows, Mac, and Linux using Pyenv!