Last Updated: February 25, 2016
·
14.02K
· pykler

Running iPython cleanly inside a virtualenv

If you run ipython directly on the commandline without having it installed inside the virtualenv you are working on you may get some awkward behaviour since it iPython will be running a mix of environments (system wide + virtualenv) ... normally you would see an warning as follows

WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
Python 2.7.3 (default, Jan  2 2013, 13:56:14)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.  
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

To avoid this I use an alias that makes sure iPython is loaded from inside the venv ... here it is:

alias ipy="python -c 'import IPython; IPython.frontend.terminal.ipapp.launch_new_instance()'"

Edit: as of ipython 1.0 this is the new alias

alias ipy="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"

now running ipy I will know for sure that I am running within the venv.

3 Responses
Add your response

Just a small comment: you can add this line to ~/.bash_profile, restart bash and now it works every time!

over 1 year ago ·

Thanks for sharing, solved my problem!

over 1 year ago ·

Genius! This has been driving me mad for days.

over 1 year ago ·