In order to solve this issue, IPython comes along to save the day.
IPython provides a way to include additional code to be executed when IPython‘s shell is started.
First, the small script that allows IPython to get virtualenv‘s packages into it’s sys.path.
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
Save the above snippet into your user’s IPython configuration directory ~/.ipython. Save it has, for example, virtualenv.py.
Now, as IPython‘s documentation suggests, edit ~/.ipython/ipy_user_conf.py and somewhere inside the main() function add:
def main(): execf('~/.ipython/virtualenv.py')
Now, the next time you start IPython‘s shell, if you’ve activated your virtualenv‘s environment, you’ll also get the packages from there too.
Develop:user@machine:~$ ipython VIRTUAL_ENV -> /home/user/.virtual_python/lib/python2.5/site-packages In [1]:
And that’s it, there you have your virtualenv aware IPython shell!
There’s also the possibility to run:
easy_install ipython
from within your virtualenv :)
Comment by Alexandre Bourget — Apr 23, 2009 1:42:26 PM | # - re
Heh, I didn’t tried that one :) Thank!
Comment by s0undt3ch — Apr 24, 2009 4:31:07 PM | # - re
What if you want to use ipython with more than one virtualenv? By default, any ipython egg that is installed will look for config in ~/.ipython, right?
Comment by Dan Fuchs — Apr 26, 2009 10:16:12 PM | # - re
thanks!!!! i was looking exactly for this!!
Comment by elsonidoq — Sep 6, 2009 3:41:02 PM | # - re
Windows version:
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’,
‘site-packages’)
site.addsitedir(virtual_env)
print ‘VIRTUAL_ENV ->’, virtual_env
del virtual_env
del site, environ, join, version_info
Comment by Randy Syring — Sep 30, 2009 6:31:19 PM | # - re
[…] and yolk. Everything else (so far) is getting put into a virtual environment. Also, now that I’ve figured out how to get virtualenv and ipython to play nice , isolated development is running smooth as silk. […]
Pingback by Virtualenv useful on Mac, not so much on PC | insomnihack — Nov 8, 2009 7:59:49 PM | # - re
I suggest putting
sys.path.insert(0, virtual_env)
before
site.addsitedir(virtual_env)
for virtual environments that don’t exclude system-wide Python packages. This way, IPython will always try to import packages from the virtualenv, first, then the system, second, which is the desired behavior. Without the insert, the virtualenv packages may be “overridden” by the system packages.
Comment by Chris Lasher — Nov 28, 2009 12:00:19 AM | # - re
Simpler solution:
alias ipython=”python -c ‘import IPython; IPython.Shell.IPShell().mainloop()’”
Comment by Carl Meyer — Feb 10, 2010 9:49:14 PM | # - re
Thanks very much, Carl, and the original author. This saved me very much time, and taught me a few more things about how IPython works. Your solution combined with virtualenvwrapper’s hooks have just made my life that much simpler.
Comment by Keith Solademi — Feb 28, 2010 3:43:04 AM | # - re