In this blog post, we will discuss how to use pyenv and pyenv-virtualenv, two powerful tools that can help you manage multiple Python versions and virtual environments with ease. We will cover installation instructions for various platforms, such as Mac and Linux, and discuss the most frequently used features of these tools. By the end, you’ll have a solid understanding of how to use these tools effectively in your development workflow.

I. Overview

pyenv is a powerful version management tool for Python, allowing you to install and switch between multiple Python versions with ease. pyenv-virtualenv is an extension to pyenv that enables you to manage multiple virtual environments. These tools are particularly useful when working on multiple projects with different dependencies and Python versions.

II. Installation

Mac

To install pyenv and pyenv-virtualenv on macOS, you can use Homebrew:

brew update
brew install pyenv
brew install pyenv-virtualenv

After installation, add the following lines to your shell configuration file (.bashrc, .zshrc, etc.):

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi
if command -v pyenv-virtualenv-init 1>/dev/null 2>&1; then
  eval "$(pyenv virtualenv-init -)"
fi

Linux

To install pyenv and pyenv-virtualenv on Linux, first clone the repositories and add them to your PATH:

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

Next, add the following lines to your shell configuration file (.bashrc, .zshrc, etc.):

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

III. Usage

1. Install a Python version

To install a specific Python version, use the install command:

pyenv install 3.9.5

2. List available Python versions

To see all the installed Python versions, use the versions command:

pyenv versions

3. Set the global Python version

To set the global Python version, use the global command:

pyenv global 3.9.5

4. Set the local Python version

To set the local Python version for a specific project, use the local command within the project directory:

pyenv local 3.8.10

5. Check the current Python version

To check the current Python version, use the version command:

pyenv version

6. Create a virtual environment

To create a new virtual environment with pyenv-virtualenv, use the virtualenv command:

pyenv virtualenv 3.9.5 my-project-env

7. Activate a virtual environment

To activate a virtual environment, use the activate command:

pyenv activate my-project-env

8. Deactivate a virtual environment

To deactivate the current virtual environment, use the deactivate command:

pyenv deactivate

9. List available virtual environments

To list all the virtual environments you’ve created, use the virtualenvs command:

pyenv virtualenvs

10. Remove a virtual environment

To remove a virtual environment, use the uninstall command:

pyenv uninstall my-project-env

Bonus: Rehash

Whenever you install a new Python package with executable scripts, it’s essential to run the rehash command to update the shims, ensuring the new scripts are available:

pyenv rehash

IV. Conclusion

pyenv and pyenv-virtualenv are invaluable tools for managing multiple Python versions and virtual environments in your development workflow. With the features discussed in this guide, you’ll be well-equipped to work on multiple projects with varying dependencies and Python versions. Embrace these tools to maintain a clean and organized development environment, enhancing your productivity and reducing the risk of dependency conflicts.