Setting Up Virtual Environments for Python

I’ve been greatly encouraged by colleagues and friends to try working on a MacBook instead of a Windows machine. Given that there is no cost to me, since the computer is provided by my company, I thought I’d give it a try. Thus far, I’ve been enjoying it greatly. And one of my favorite things that I’ve learned thus far is the ease with which one can build and manage virtual environments.

A virtual environment is different from a virtual machine. A virtual machine requires allocation of disk space and RAM. A virtual environment is simply an isolated working copy of Python. The two main benefits to setting up virtual environments are

  • that you can work in two completely different environments simultaneously and
  • you create accountability and tracking for changes made to the environment that can later be replicated by a team member if they decide to pick up on your project

Now, if you’re like me when I first heard about this, you’re probably feeling a bit intimidated. No worries! Let’s do a quick step-by-step on how to set all of this up so that you can start configuring and using your own virtual environments.

  1. Start by installing the virtualenv module on your machine via Terminal. To do this, use the command
    $ pip install virtualenv

    If you don’t have pip already installed, I would encourage you to install it using the following command:

    $ sudo easy_install pip

    It’ll definitely come in great aid later when you’re installing packages in all your new environments.

    Installing virtualenv

  2. Now that you have the virtualenv module installed on your machine, you can start setting up virtual environments. Yes, it’s that simple!To start off, it’s best practice that you setup your virtual machine within the folder of the project that you are working on. So, through the Terminal, navigate to the folder where your project is stored.Navigating to Directories via Terminal
  3. Once within the directory, create the virtual environment using the command
    $ virtualenv project

    Another best practice is to name your virtual environment the name of the project on which you are working. In my case, the name project is befitting, but make sure to select the right name for yourself.

    Note that if you have multiple instances of Python installed on your machine, you can also specify which instance you would like to be used in this particular virtual environment. We do not specify that in the command above, so the version used in this particular virtual environment will just be the default version on the machine.

  4. Now, before we start using our new environment, we need to configure it with modules specific to our project. To do so, activate the environment using the command
    $ source project/bin/activate

    Make sure to change the first word after “source” to the name of your own virtual environment. The name of your environment will be added in front of your command line once you’ve activated the environment.

    When your virtual environment is active, configure it as you wish. In this example, I install module elasticsearch in my project virtual environment:

    Creating Your Virtual Environment

  5. Once you complete configuring your environment, quit configuration by using the command
    $ deactivate
  6. To verify that your virtual environment is indeed different from your defaulted edition of Python, you can run a simple query testing for new modules in the defaulted Python version and then again in the virtual environment version. Note in my example that elasticsearch is present in my virtual environment, but not in my defaulted version of Python:Testing Virtual Environment Against Default Python Version
  7. Now that you have your virtual environment setup, you can run scripts specific to your project within that virtual environment without having to modify your defaulted version or affecting other projects you may be working on. Additionally, you can share your virtual environment with collaborating colleagues. To share your environment, while in your project’s directory within the Terminal, use the following command to create a requirements file:
    $  pip freeze > requirements.txt

    This will create a file in your project folder with all the modules present in your virtual environment.

    Creating the requirements.txt File

    When collaborating with other people, send them this requirements files, have them place it in their project directory, then navigate to that directory via their Terminal, and run the following command:

    $  pip install -r requirements.txt

    This will set up a matching virtual environment on their machine so that they can run your files without any issues.

For those seeking to learn more about virtual environments and all the things you can do with them, I highly encourage you to check out the full documentation for the module, which can be found here. A big thank you to my colleague who took time out of her day today to educate me about this.

2 thoughts on “Setting Up Virtual Environments for Python

  1. Pingback: Running Scripts in Pentaho Kettle, the Sequel | Moran Nachum

  2. Pingback: Setting Up a Virtual Environment for Python 3 | Moran Nachum

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: