Python Development on macOS with pyenv-virtualenv

Jordan Gardner
Jul 12, 2017 · 2 min read

UPDATED: May 31, 2020

Check out this post on our new website,

This post is Part 2. Make sure you read Part 1: Python Development on macOS with pyenv.

At this point you should have pyenv installed. You can now quickly install and switch between any version of Python that you heart desires. But what if you want to use the same version of Python for two projects that use different versions of Django?!

We use pyenv-virtualenv.

From the docs:

pyenv-virtualenv is a pyenv plugin that provides features to manage virtualenvs and conda environments for Python on UNIX-like systems.

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"
# 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.

pyenv virtualenv <python-version> <name>

This command will create a new virtual environment with whatever name you supply and use the specified Python version. Let’s create a new virtual environment using Python 3.7.2.

# Install Python 3.7.2
pyenv install 3.7.2
# Create a new virtual environment
pyenv virtualenv 3.7.2 django-2.2

This creates a new virtual environment named django-2.2.

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

pyenv activate django-2.2

We are now in our virtual environment and we can install Django 2.2:

pip install django==2.2

This version of Django is only available in our virtual environment and it won’t conflict with anything else!

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

pyenv virtualenv 3.7.2 django-1.11

Now let’s activate it:

pyenv activate django-1.11

And install our desired version of Django:

pip install django=1.11

Hot dog! We have the right version for this project and it doesn’t conflict with anything.

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.

Anytime you change directories, pyenv looks for a .python-version file and uses the specified Python version or virtual environment!

cd into your project directory and run:

pyenv local django-2.2

Notice that you now have a .python-version file in your project. The next time you cd into the project, pyenv will read this file and activate the django-2.2 virtual environment for you!


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

python every day

Everyday Python from someone who uses Python every day

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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