I dabble with Python programming every now and then. Nothing serious, just casual n00bz stuff. Along the way, I stumbled across virtualenv. In a nutshell, it's a tool to create isolated Python environments. Python, like most programming languages, have 3rd-party modules (or libraries) that can be installed and used within your very own projects. Using pip makes it easy to do (granting pip is installed on your system).
Installing 3rd-party modules directly onto your bare-metal system may be fine for most cases, so you might be thinking why isolated Python environments? Well, if you're like me, I prefer to alter my base system as less as possible in terms of installed software. But from the top of my head, here are some reasons to consider using virtualenv.
- You don't have root privileges in your current system to issue a sudo pip install module command
- You work with a lot of Python projects that use different combinations of 3rd-party modules
- You are concerned that installing all 3rd-party modules may cause conflicts with each other
- You are OCD, like I am
- You like anything "virtual"...
The first 3 reasons could be valid...the last 2 can be ignored. Anyway, here's a use-case to illustrate my workflow.
Aside from Python, I am also dabbling with a bit of web development every now and then; again, nothing serious. So, I came across the Django framework. It's one of those 3rd-party modules that can be installed by pip install django. Every web developer knows the infamous web development stack: LAMP (Linux, Apache, MySQL and PHP). As I'm also doing some n00bz stuff with LAMP, I've decided to setup a virtual machine as my test development server (again, because I didn't want to install them to my main machine). But virtual machines can be taxing to your host computer and if I made another for a Django test development server, then I'd have 2 VMs eating up my resources. So virtualenv is a nice alternative.
Installation
There are many ways of installing virtualenv to your system. As mentioned, I preferred to install it locally, like so.
~ $ mkdir -p virtualenv venvs ~ $ cd virtualenv ~/virtualenv $ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz ~/virtualenv $ tar xvfz virtualenv-X.X.tar.gz ~/virtualenv $ cd ~/venvs ~/venvs $ python ~/virtualenv/virtualenv-X.X/virtualenv.py django-dev
- mkdir -p virtualenv venvs creates 2 directories, virtualenv and venvs under my home directory
- change to the virtualenv directory
- using curl, download virtualenv with version X.X (which, at the time of this post, is 1.11.2)
- extract the archive; it will be under the folder of virtualenv-X.X
- go the the ~/venvs directory; any directory (you have access to) will do, this is just how I personally organize my virtual environments
For global installations, it's just a matter of using your Linux distribution's repos. There are 2 ways to do this. I am using Fedora currently so I will only be able to show how in said distro...
- Install pip from package manager (sudo yum install python-pip) and from pip, install virtualenv (sudo pip install virtualenv)
- Install virtualenv from package manager (sudo yum install python-virtualenv) and create virtual environments to your hearts, and HDD's, content.
Creating a Virtual Environment
I've already demonstrated above how to create a virtual environment. It's just simply going to a directory where you want it created, then do...
~/venvs $ python ~/path/to/virtualenv.py name-of-virtual-environment
Note that if you installed it globally then it's as simple as issuing the command virtualenv name-of-virtual-environment inside your preferred directory.
Activating a Virtual Environment
A virtual environment needs to be activated before it is used. It's simple.
~ $ cd venvs/django-dev ~/venvs/django-dev $ source bin/activate (django-dev) ~/venvs/django-dev $
- change directory to venvs/django-dev
- source (a command I don't fully grok) activates the virtual environment
- once a virtual environment is activated, then it's name is appended to the beginning of your prompt; in the example, it's (django-dev)
Installing Stuff into a Virtual Environment
While in a virtual environment, all 3rd-party modules installed, via pip, will only exist in that virtual environment. So, for the example django-dev virtual environment, I can do...
(django-dev) ~/venvs/django-dev $ pip install django ... (django-dev) ~/venvs/django-dev $ django-admin.py startproject mydjangosite
...and it will only be installed there. Once I exit from the virtual environment, the command django-admin.py will not exist.
Exiting a Virtual Environment
Easy as pie.
(django-dev) ~/venvs/django-dev $ deactivate ~/venvs/django-dev $
- while inside a virtual environment, issuing the deactivate command exits it
- this is confirmed by the now missing virtual environment name from the prefix of your prompt
Conclusion
virtualenv is a pretty handy tool for Python (and Python-based) development. It gives you versatility. You can make one virtual environment for Django development and another for Flask, another great web framework based on Python. It can also be of great help when testing out new versions of modules e.g. you have a stable Django app running in version 1.11.2 and then Django 1.12 comes out and you want to test it first to see if your app will be as stable running in it. Using virtualenv, it's just as easy as spinning up a new virtual environment for Django 1.12 and copying all your code for testing.
Thanks for reading. And happy Python-ing, or Django-ing, or whatever Python development you're into.
No comments:
Post a Comment