Skip to content

Setup and use Python’s (v3.x) virtual environments using pyvenv

Michael Hulse edited this page Nov 14, 2019 · 2 revisions

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

Development notes

Assuming you’re using Homebrew as a package manager.

Python3 boilerplate

Install Python:

$ brew install python3

Create the “FOO” environment:

$ pyvenv ~/.virtualenvs/FOO

Activate:

$ source ~/.virtualenvs/FOO/bin/activate

Deactivate (when activated):

$ deactivate

Note, pip comes with Python 3.4 pyvenv:

$ which pip
~/.virtualenvs/FOO/bin/pip

Nice!

To make activation quick, add an alias to your .bash_profile:

# Simplify pyvenv activation for "FOO" environment:
alias FOO='source ~/.virtualenvs/gutenberg/bin/activate'

And then reload:

source ~/.bash_profile

Now:

$ FOO

… which activates your “FOO” virtual environment; now you can install packages, using pip, into your environment’s site-packages.

Clone this wiki locally