What is virtualenv?
virtualenv is a tool that was created by the Python Packaging Authority to allow Python developers to keep the respective development environments they're using for various projects isolated from each other to avoid conflicts that tend to arise when projects share packages and dependencies.
For Python developers (as with many other web programming languages), the scenario of working on multiple projects simultaneously that require different configurations is a common one. virtualenv solves the problem of keeping those environments from stumbling over each other. With virtualenv, developers have a much lighter way to essentially run multiple (virtual) Python installations on their development machine.
Installing and Using virtualenv
Installing virtualenv is as simple as using the pip installer that comes with Python to run the virtualenv install as follows:
pip install virtualenv
To create a new virtual environment, you simply run (inside of the folder where you'll be using the environment):
To activate any of the environments you've created via virtualenv, you use:
Whatever updates (installing packages, etc.) you make to the active environment are isolated to that specific environment.
We've covered enough about using virtualenv for creating isolated Python to now make a comparison between that tool and Nanobox.
This video from Cory Schafer provides a more detailed look at how to use virtualenv.
Isolated Python Development Environments with Nanobox
Nanobox is a development platform that functions as three important things for Python web developers:
- a local development environment
- an automated deployment tool for getting an application to the cloud
- a production app management platform for monitoring and scaling
Similar to virtualenv, the Nanobox platform keeps project environments separate and isolated. In addition, The local development part of Nanobox - Nanobox Desktop - was designed to solve several other dev environment-related problems in addition to simply keeping local environments isolated from each other, including:
- eliminating environment cruft
- eliminating environment inconsistency among dev team members
- facilitating local staging environments
- allowing devs to pause and resume environments
- making it easy to maintain legacy applications
- getting new developer hires up and running quickly
After you've created your boxfile.yml, you'll create a requirements.txt file that tells Nanobox which packages you'll need for this Python project.
Once the requirements.txt file is in place, you install your packages by running:
nanobox run pip install
At this point, you now have a Python project with the dependencies you need running inside of a Docker container created by Nanobox.
Advantages of Using Nanobox Over virtualenv
Considered purely in the base context of what virtualenv was created to do - simply keep Python environments isolated to avoid conflicts, Nanobox is very similar. Nanobox has additionaly functionality beyond what virtualenv does, fitting isolated dev environments into the overall dev to production workflow.
Nanobox development environments include not just the local Python environment, but also everything else that comprises the full environment for an app. This includes other services that are used by that particular project, including data components, caching, web/logging servers, and others.
Nanobox also makes it very easy to share environments among developers. With Nanobox, the environment actually lives with the codebase, so when you share your codebase, you also share the environment. With virtualenv alone, sharing Python environments normally requires extra manual work. With virtualenv, sharing the project from your source control repository would involve setting up something that describes the environment and puts it in place with the codebase. You'd need something like Docker Compose or Vagrant.
Likely the most important advantage of using Nanobox over virtualenv is that the Nanobox platform makes it easy to deploy apps built from the isolated development environment to production servers without needing devops experience. Nanobox ensures that the environment you use for local development is the same one that will exist in your testing, staging, and production environments.
For Python developers who are simply concerned about keeping their various Python projects from conflicting, virtualenv is simple to use and does the trick. For those who are more concerned about the whole workflow from dev to production, especially dev team managers and product/project managers concerned with developer productivity, Nanobox is a better option.
To set up a project with Nanobox, first register for an account. Then download and install Nanobox Desktop from your dashboard. You can use Nanobox Desktop for free. To deploy your Python applications to the cloud and manage them in production, there is a $10/month subscription cost for individual users.