Creating a Python Virtual Environment Using virtualenv

There are loads of tutorials out there for getting started with Python, but all of the ones I’ve come across miss some critical information. It’s often that the writer makes unfounded assumptions and beginners are left to piece and hobble together scraps from across the web to accomplish a simple task.

In the following, I’ll give you some basics about creating a Python virtual environment. I really do mean basics, things that I’m not going to assume you know about this process. Use this as a reference when you want to revisit how to start a new project or you blank on a terminal command.

By the way, I am working on a Mac, so your commands may look slightly different on other OS.

What you will need:

  1. A code editor. I use Sublime.
  2. Basic knowledge of your terminal.
  3. Python installed. If you have a Mac, you already have a version of Python installed.

If you want to check for Python on your computer, use the following command: python -V

My output for this command was: Python 2.7.16

What’s a virtual environment?

A virtual environment is a place for you to install packages that are specific to your project. Packages give your code special abilities. For example, I built a Slack bot called Nice Vegan Bot. You can type @niceveganbot and “fact please” in any Slack channel and you will be returned a fact about veganism from my MySQL database. For this bot, I used the package slackClient, which allowed me to feed my application with my Slack API token.

I came from a background in JavaScript where I installed packages using npm (and yarn a few times). When you’re working with Python, it’s highly suggested that you use a virtual environment for your project rather than install packages globally to your machine. A virtual environment simply keeps your dependencies isolated to your specific project. Get in the practice of using a virtual environment for all of your Python projects and you will alleviate the risk of package conflicts on your machine.

How to create a virtual environment.

Let’s make a new Python project and add a virtual environment.

In the following instructions, you will be asked to run commands that start with pip. pip is the package installer for Python. You already have pip if you have Python, so go ahead and run those commands.

Install virtualenv, a popular virtual environment package. Skip this step if you already have virtualenv installed. This command installs virtualenv globally so that you can add it to as many Python projects as you’d like.

pip install virtualenv

For reference, you can run this command to check which pip packages you have installed globally: pip list

CD into your Python project.

cd sample-application

Create a virtual environment specific to this project. The second part of the command below is anything we want to name the environment. So in this case, I called it ENV.

virtualenv ENV

Now we need to activate our new environment, ENV. Run the following command. If you named your environment anything other than ENV, replace ENV with the name of your environment.

source ENV/bin/activate

Now you’ll know that your environment is running when it appears in your project command line. You’ll see that the environment is listed in parentheses.

(ENV) Falons-MacBook-Pro:sample-application falondarville$

At this point, you can go ahead and install the packages you need for this specific project. The Python Package Index is your go-to reference for pip packages available.

Finally, to stop your environment, run deactivate

Now you know how to add a virtual environment to your Python project to keep your dependencies separate and potentially avoid package conflicts.