Making iPython Virtualenv Aware

iPython is a great replacement for the standard Python shell, but, out of the box, it doesn’t play well with virtualenv.

This can be fixed with a couple of changes:

In the terminal generate an iPython default config file:

ipython profile create

Open the newly generated file:

vi ~/.ipython/profile_default/ipython_config.py

Add the following code at the end of the ipython_config.py file:

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 'VIRTUAL_ENV ->', virtual_env
    del virtual_env
del site, environ, join, version_info

And that’s it, iPython should now be virtualenv aware.

4 September, 2012 — 4 comments

4 Comments


Paul McCann 30 November, 2012 03:53

Thanks! That came in handy. On my installation, the ipython config directory is in ~/.config/ipython.



Thomas Kluyver 2 December, 2013 21:42

Recent versions of IPython now do this automatically, so you shouldn’t need to do it in config files any more.



Andrew 13 May, 2016 16:03

This fails for me on ubuntu 14.04:

$ ipython profile create

[TerminalIPythonApp] WARNING | File not found: u’profile’

$ ipython –version
1.2.1

Creating the file manually with just your posted contents did not have the desired effect either, unfortunately.



python – Calling IPython from a virtualenv-ThrowExceptions – ThrowExceptions 19 March, 2020 13:11

[…] understand that IPython is not virtualenv-aware and that the most logical solution to this is to install ipython in each virtualenv seperately […]



Comments are closed for this post