Building a Virtual Python Environment

by Akshay Ranganath
2716

When embarking on a devOps journey, getting a consistent development environment is the key. In this post, I show how to setup a virtual environment and install the correct libraries necessary for a project.

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. The tool in python is called virtualenv.

To work with isolated environments, the basic steps are as follows:

Initial setup:

  1. Install the virtualenv tool.
  2. Initialize your environment
  3. Install dependencies
  4. Create the requirements.txt to remember the dependencies
  5. Check code and the requirements.txt file into code repository

Developer installs:

  1. Create virtual environment
  2. Get the latest code
  3. Install dependencies
  4. Start working on code

Let’s step through these in detail.

Initial Setup

You or one of the developers on your team can kickstart the process by installing the virtualenv tool.

The next step is to initialize virtual environment on the project folder.

This step will install python, pip, and basic libraries within the project folder, myproject. After version 1.7 of virtualenv, it will use the option –no-site-packages by default. It means that virtualenv will not install the libraries available globally. This will help ensure that the project package contains the bare minimum libraries and the libraries installed for the specific project.

After this, you will need to activate the virtual environment to start working on the project.

Once you do this, your prompt will change to show that you are now working in a virtual environment. It should look something like this.

Now, go ahead and install the libraries that may be required for the project. If all the dependencies can be determined at this stage, it will make it easier to replicate the environment. The dependency list can always be updated as I’ll show later.

Once you’re done with the installations, create a list of the libraries and their versions.

The requirements.txt file will hold the necessary dependencies for your project. In one of my projects, it looks like this:

Check this file into your repository along with the project code. If anyone adds functionality which requires a new library, they add it to this requirements.txt file. That way, once other developers pull the update from the source code repository, they’ll be aware of the new dependencies and able to install them easily.

Developer Install

For each developer, the project setup is now straight-forward. To create this virtual environment, first they need to install virtualenv.

Then create a folder and activate virtual environment.

After this, they’ll need to check-out the code from code repository. Assuming it is a git repo this will just be a git clone command.

Now navigate to this sub-folder:

Install the dependencies:

And that’s it! Your developer machine is now all set for coding.

Additional Resources

This page on a Hitchhiker’s Guide to Python is an excellent reference for anything virtualenv.

{This blog was originally published as “DevOps : Building a Python virtual environment”.}

Categories: DevOps How To

Suggested Article

LEAVE A REPLY