Use Virtualenv with IPython 0.11

If you are a Python hacker, most likely you will be using a virtualenv for your development environment, you are probably an iPython user. Unluckily, IPython doesn't pick the virtualenv paths by default and you will have to tweak it a little bit to make it work. Basically, this a slightly tweaked version of this article but tailored for the recent version of IPython 0.11.

You simply need to drop this snippet of code into the iPython configuration directory as illustrated below so that IPython can add the virtualenv paths.

import site  
from os import environ  
from os.path import join  
from sys import version_info

if 'VIRTUAL_ENV' in environ:  
    virtual_env = join(environ.get('VIRTUAL_ENV'),
                       'lib',
                       'python%d.%d' % version_info[:2],
                       'site-packages')
    site.addsitedir(virtual_env)
    print 'Using Virtualenv =>', virtual_env
    del virtual_env
del site, environ, join, version_info  

Drop that snippet into a file named ipython_config.py in the following path if you are using Mac OS X or Linux (tested on Ubuntu natty):

~/.ipython/profile_default/ipython_config.py

Happy Hacking!

comments powered by Disqus