Using VIRTUALENV in python

virtualenv

I wanted to blog about virtualenv which is really an important tool to use when installing Python 2.x or 3.x. Centos, for exmaple, comes with its own version of Python. Install a newer Python version and you have broken yum. This is not good, but this is where virtualenv comes in. In essence, virtualenv allows you to run multiple python environments on one server which in turn allows you to run different libraries that will not conflict with each python environment. Say I have a mysql library that needs to be at a specific version – I have installed the mysql python package. If I use virtualenv, that mysql python package will only apply to the virtualenv python environment. It therefore is a way to keep your libraries separate. Now you can still use your main python library for common libraries, but virtualenv just helps separate each python environment – good also when mulitple developers are working on the one server.

Here is how to set up a virtualenv for Python (pre-requisite is that you have your version of python installed)

1 – Make sure you have virtualenv installed.

yum search python-virtualenv

2 – Create a directory to install your python environment

mkdir ~/my_python_app/

3 -Run the virtualenv –<directory from step 2>

virtualenv ~/my_python_app/

Here’s the output for  steps 2 & 3

[root@centos ~]# mkdir my_python_app
[root@centos ~]# cd my_python_app/
[root@centos my_python_app]# virtualenv ~/my_python_app/
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /root/my_python_app/bin/python2.7
Also creating executable in /root/my_python_app/bin/python
Installing Setuptools............
Installing Pip...................
done.

And there you have it – an independent python environment. There is more that you should do to really customise the environment, but that’s all you need to do to initially get yourself up and running.

I do however want to highlight a useful option, which can be found using virtualenv –help:

-p PYTHON_EXE, --python=PYTHON_EXE
        The Python interpreter to use, e.g.,
        --python=python2.5 will use the python2.5 interpreter
        to create the new environment.  The default is the
        interpreter that virtualenv was installed with
        (/usr/local/bin/python2.7)

This option allows you to create a python environment using a different python version. This is really good if you want your python environment to be independent but use different version of python, 2.6, 3.x etc.

Activating your environment.

So once you have configured your python environment, you need to activate it so you can work on it. Here is how to do this:

[root@centos my_python_app]# source /root/my_python_app/bin/activate

Once you have done this, you will see the prompt change as follows:

(my_python_app)[root@v28388581sl0001 my_python_app]#

The (my_python_app) just shows you what virtualenv is activated.

Once you are activated, you can start using pip to install your libraries, code in python, etc. Once you are finished, you just need to deactivate the virtualenv, as follows:

(my_python_app)[root@centos my_python_app]# deactivate
[root@centos my_python_app]#

Now you can see your prompt has gone back to normal and the active virtual environment has been removed.

So, how do you run a python script using a specific python environment? Well, you do not need to activate the python virtualenv, you just specify the full path to the python interpreter in the bin folder within the virtualenv.

We installed a virtualenv in /root/my_python_app, so the python exe or interpreter (which ever you fancy calling it) will be found in the bin folder, as follows:

[root@centos /]# /root/my_python_app/bin/python
Python 2.7.5 (default, Oct 24 2013, 15:46:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Notice that the version is Python 2.7.5 – if I had specified a different python version, that would reflect when calling the python interpreter in the bin folder. All you need to do is specify the full path followed by the script you wish to run.

TIP:  However, I can give you a easy tip here. Rather than specify the full path, update your bash profile or better still, /etc/bashrc as follows:

alias python_v1=/root/my_python_app/bin/python

Happy Python coding – and please get to use virtualenv. It will save you hassle.

Advertisements

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