Running multiple versions of Python on your machine using pyenv

I needed to test sending emails with SendGrid using Python, but as per the docs, it needed Python 3.8 be able to do so. At that time, my machine had Python 3.11 installed. So, I needed to switch my Python version for the assignment.

This is when we use Pyenv.

Pyenv is available for download easily using Homebrew for Mac and Linux.
To install, run:

brew update
brew install pyenv

Check all available Python versions available for download using pyenv:

pyenv install --list

Install a particular version of Python:

pyenv install 3.8

Set the Python version for your project by running the command below from the project's root directory:

pyenv local 3.8

Check the local version in your current project:

pyenv local

Now create a virtual environment for the selected Python version:

pyenv exec python -m venv .env

Activate the virtual environment:

source .env/bin/activate