Python Development on macOS with pyenv-virtualenv

Installation Guide

Follow these steps to install pyenv-virtualenv on macOS. We’ll be installing pyenv-virtualenv via Homebrew so make sure you have it installed.

# Install Homebrew if it isn't already available
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install pyenv-virtualenv
brew install pyenv-virtualenv
# Add pyenv-virtualenv initializer to shell startup script
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
# Reload your profile
source ~/.bash_profile

Usage Guide

Yay! pyenv-virtualenv is installed and functioning. Now we can create virtual environments that use the same version of Python, but have their own set of pip installed packages.

Create Virtual Environment

pyenv virtualenv <python-version> <name>
# Install Python 3.9.4
pyenv install 3.9.4
# Create a new virtual environment
pyenv virtualenv 3.9.4 django-3.2

Activate Virtual Environment

Now that we’ve created our virtual environment, let’s activate it:

pyenv activate django-3.2
pip install django==3.2

Just For Fun

Let’s go ahead and create another virtual environment for our project that uses Django 3.1 and see what happens.

pyenv virtualenv 3.9.4 django-3.1
pyenv activate django-3.1
pip install django=3.1

Activate Environment Automatically

As long as you followed the installation instructions and included the eval "$(pyenv virtualenv-init -)" line in your ~/.bash_profile, pyenv-virtualenv has the ability to automatically activate/deactivate your environment for you.

pyenv local django-3.2

Conclusion

Make sure to read the docs to better understand what both pyenv and pyenv-virtualenv can do. Now go enjoy developing without dependency conflicts!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store