Managing Python Versions and Virtual Environments with pyenv

Set up pyenv and pyenv-virtualenv on macOS to switch Python versions and activate project environments automatically.

On this page

Managing Python Versions and Virtual Environments with pyenv

While setting up a new computer, I needed to install Python. I used to run python -m venv venv for each project. This time I tried managing Python versions and virtual environments with pyenv and pyenv-virtualenv, so I wrote down the setup.

Installation and basic configuration

Install pyenv and pyenv-virtualenv

Use Homebrew to install the core tool and the virtual environment plugin.

brew install pyenv
brew install pyenv-virtualenv
Bash

Initialize pyenv in Zsh

Add the initialization commands to ~/.zshrc so pyenv can switch Python versions and pyenv-virtualenv can activate and deactivate environments automatically.

Add these lines to ~/.zshrc

# PYENV / PYENV-VIRTUALENV 初始化
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Text

WARNING

If you use Powerlevel10k like I do, check where you put these commands. Load your shell configuration first, and make sure pyenv init runs before pyenv virtualenv-init.

I usually place them after source $ZSH/oh-my-zsh.sh and before [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh.

Apply the configuration

Save the file, then reload it.

source ~/.zshrc
Bash

Create and use a virtual environment

Install a Python version

Install a base Python version before creating an environment.

pyenv install 3.13.0
Bash

NOTE

Install any version you want.

Create a virtual environment

Create an environment named venv from the Python version you just installed.

pyenv virtualenv <Python Version> <Virtual Environment Name>
Bash

NOTE

I use venv for the example. For real projects, use a name such as my-blog-api-env or testing-sandbox-env because environment names must be unique.

pyenv virtualenv 3.13.0 venv
Bash

NOTE

The command stores the environment under pyenv, for example at ~/.pyenv/versions/3.13.0/envs/venv. The current project folder stays empty.

List your environments

List installed Python versions and virtual environments together.

pyenv versions
Bash

Or list only the virtual environments.

pyenv virtualenvs
Bash

NOTE

The output includes the base version, such as 3.13.0, and the environment name, such as venv. Output of pyenv virtualenvs

From the project directory, run pyenv local to associate that directory with a particular virtual environment.

cd /path/to/your/project
pyenv local venv
Bash
  • This example uses the venv environment created earlier.
  • The command writes a hidden .python-version file in the project directory with venv as its contents.
  • With pyenv virtualenv-init configured, entering the directory activates the environment. Your prompt shows (venv).

Shell prompt with the project's virtual environment active

TIP

pyenv checks the shell setting first, then the local setting, then the global setting.

  • pyenv shell <version> applies to the current terminal session and sets PYENV_VERSION.
  • pyenv local <version> applies to the directory and its descendants through the project's .python-version file.
  • pyenv global <version> is the default when neither setting applies. pyenv stores it in $(pyenv root)/version.

Manage and remove environments

Deactivate the environment

  • Automatically. Leave the project directory, for example with cd ...

  • Manually. Switch back to the global version from inside the project.

    pyenv deactivate
    Bash

Remove a project's environment setting

To keep the virtual environment while clearing the project's association, remove the local setting.

pyenv local --unset
# 等同於手動刪除 .python-version 檔案
Bash

Delete a virtual environment

When you no longer need an environment, use pyenv uninstall to remove it and its installed packages.

WARNING

Leave any directory that uses the environment before running the command.

cd ..  # 先退出專案資料夾
pyenv uninstall venv
Bash

pyenv asks for two confirmations during uninstall.

  1. pyenv: remove .../versions/venv? (y/N) removes the shortcut.
  2. pyenv-virtualenv: remove .../3.13.0/envs/venv? (y/N) removes the environment directory.

The two pyenv uninstall prompts

Closing note

That's the setup I used on my new Mac. With the environment linked to the project, entering the directory is enough to activate it.