Python Virtual Environments
Oct. 26, 2016
Managing Virtual Environments
I recently had a conversation with my youngest son who is majoring in Computer Science & Engineering at the University of California, Irvine...go "Anteaters!" I have been very impressed with how much he learned about python programming during his freshman year. I made an off-hand comment about needing to create a virtual environment for one of my projects, and he asked why stating he never had a need to use virtual environments. Of course, I was properly aghast at his nonchalance! It never occurred to me to have "The Talk" with him regarding the importance of safe computing. I shudder to think of having all my various projects depending on a single environment with no protection. However, I realized the concept of virtual environments can be daunting for those who have never used them. If you are one of those people please read on and I will attempt to quickly dispel the shroud of mystery.
There are a number of different tools and techniques available for creating virtual environments;
available tools include
pyvenv. Both are easy to use but I recommend using pyvenv
because virtualenv has been deprecated. Creating a virtual environment is easy. Let's assume we
have a django project called StormChaser and we want to create a virtual environment called stormenv. Further, we want to ensure our new virual environment has django installed (it's standard practice to manage project dependencies in a file called requirements.txt). The steps are:
pyvenv stormenv # Create the virtual environment source stormenv/bin/activate # Activate the virtual environment pip3 install django # Install django into your virtual environment pip freeze > requirements.txt # Redirect output of freeze command to requirements.txt deactivate # Shutdown the virtual environment
The above assumes you are creating a new virtual environment from scratch (i.e. with no requirements.txt file). Keep in mind that an existing requirements.txt file can be used to create a virtual environment. This is particularly useful when developers collaborate on projects and it is important to use the same libraries for consistency purposes. When using an existing requirements file the steps are very similar:
pyvenv stormenv source stormenv/bin/activate pip3 install -r requirements.txt # Need to specify path if requirements.txt isn't in current directory deactivate
I can't stress enough how important virtual environments are for even casual programmers. I frequently install new libraries or newer versions of existing libraries. It is impossible to know the impact a new version may have on an existing project and troubleshooting the issue can be an unnecessary and time consuming process. Many application server platforms such as Heroku won't even deploy a Python application without a requirements.txt file! This blog post is being served from the Heroku platform. Below is the output generated by running the command
for the virtual environment used to deploy the CloudStorm backend deployed on Heroku:
The versions are specified after the
== and you are free to upgrade as you see fit. Or not. It's all up to you!