Install and Activate a Python Virtual Environment

Installing the virtualenv package will isolate our Python project from the system’s Python environment.

We can easily install this using home-brew:

1
brew install python-virtualenv

Now create a projects folder in our your home directory and then create a virtual environment within this folder:

1
2
3
mkdir ~/projects
cd ~/projects
virtualenv --no-site-packages venv

This creates a directory called venv within the projects directory. It installs some Python utilities within this folder and creates a directory structure to install additional tools.

Now activate the virtual environment:

1
source venv/bin/activate

The command prompt will change to reflect the fact that we are operating in a virtual environment. If you need to exit the virtual environment, you can type deactivate at any time.

^