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 Comments
Thanks! That came in handy. On my installation, the ipython config directory is in ~/.config/ipython.
Recent versions of IPython now do this automatically, so you shouldn’t need to do it in config files any more.
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.
[…] understand that IPython is not virtualenv-aware and that the most logical solution to this is to install ipython in each virtualenv seperately […]