Python [how to] : using virtualenv in your python or django project

Virtualenv.. Wait. What?

virtualenv is a tool to create isolated Python environments. The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

That is what they said, yes it is. Some python library, is sometime compatible, and sometime not. So this is the answer, IMO , virtualenv it’s just like a virtual machine, but only for python, in 1 machine you can have multiple virtual machine (cmiiw).

How to use Virtualenv

It’s so simple, just use this command :

pip install virtualenv

Now, to create your virtualenv, use this command :

root@laptop:/home/# virtualenv test
New python executable in test/bin/python
Installing setuptools............done.
Installing pip...............done.

Now, to activate your virtualenv, here’s the step :


root@laptop:/home# cd test
root@laptop:/home/test# ls
bin  include  lib
root@laptop:/home/test# source bin/activate
(test)root@laptop:/home/test#

Now, try your virtualenv :

(test)root@laptop:/home/test# python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>>
(test)root@laptop:/home/test#

It said, django module not installed in your virtualenv, so you can use some other version (ie: 1.3.1 older) django package.

Now, what is the different without using virtualenv :

root@laptop:/home/test# python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.4'

if we use the “usual” system, if you want to downgrade to older version of django, You must to uninstall django 1.4, and then install older version of django (ie: 1.3.1). It’s not great idea.

To exit from virtualenv :

(test)root@laptop:/home/test# deactivate
root@laptop:/home/test#
Advertisements

2 thoughts on “Python [how to] : using virtualenv in your python or django project

  1. […] 192 of awesome slides will tell you about Development with Django from basic, including python, virtualenv, git. You can find the code […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s