Posted Thursday, 04 March 2010 at 03:09 by Andrew Liu
Tagged: python | web development
Read more blogs...
OS: Fedora 10
When you install packages in python, they normally go into the site-packages folder, and are then available to all applications that use python. Sometimes, you dont want this to occur. You only want to install a package for yourself (or your application), and you dont want the chance that this may conflict with other applications. That's where virtualenv comes in. Virtualenv can be found at here at the python website. Another good source of information comes from the author of Virtualenv, Ian Bicking, at this post.
So how do we do this? Firstly, download virtualenv from the python website. As always, I do this as root, and download source packages into the root directory.
$ wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.3.2.tar.gz
--2009-02-28 00:13:47-- http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.3.2.tar.gz
Resolving pypi.python.org... 220.127.116.11, 2001:888:2000:d::a3
Connecting to pypi.python.org|18.104.22.168|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1027839 (1004K) [application/x-gzip]
Saving to: 'virtualenv-1.3.2.tar.gz'
100%[======================================>] 1,027,839 181K/s in 5.5s
2009-02-28 00:13:54 (181 KB/s) - 'virtualenv-1.3.2.tar.gz' saved [1027839/1027839]
$ tar xvfz virtualenv-1.3.2.tar.gz
$ cd virtualenv-1.3.2
$ python setup.py install
Finished processing dependencies for virtualenv==1.3.2
$ which virtualenv
With virtualenv installed, we now have a binary called "virtualenv" that we can call. This will create the virtual environment that we are after. Lets say we want to create a new project called "myapp". We create the environment using the command below.
$ virtualenv myapp New python executable in myapp/bin/python Installing setuptools............done.
$ cd myapp
bin include lib
$ ls bin
activate activate_this.py easy_install easy_install-2.6 python
We have a few shortcuts here in the bin directory that we need to use. The first command is the "activate" command, which will set the paths needed to use this virtual environment. I tend to like having a shortcut outside of the bin directory. So we'll create a symbolic link here.
$ ln -s bin/activate activate
activate bin include lib
And then to activate this, we need to source the file. You can use the "source activate" command, or the shortcut ". activate".
$ . activate
activate bin include lib
If successful, it will prefix your prompt with the words "(myapp)" to let you know that you are in a virtual environment. Now you can work in your isolated environment! When you are finished, naturally you should "deactivate" this virtual environment.
And this will change the prompt back to normal.