Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling IPython from a virtualenv

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

pip install ipython

So far so good. One thing I noticed is that if the system-wide copy of IPython is called from within a virtualenv using $> ipython before IPython is installed under this virtualenv, subsequent $> ipython commands will continue to bring up the system-wide ipython copy.

On the other hand, if ipython is not called prior to installing it under a virtualenv $> ipython will bring up the newly installed copy.

What is the explanation for this?

It also makes me wonder if this behavior means I should expect some trouble down the way?

like image 657
Mo Sander Avatar asked Dec 02 '13 11:12

Mo Sander


People also ask

How to open ipython in virtual environment?

As others mentioned, recent versions of ipython are virtualenv aware, so you can use your virtualenv bin activate script to run ipython using your virtualenv, e.g. $ source venv/bin/activate (venv) $ ipython WARNING: Attempting to work in a virtualenv.

How do I download from Ipython?

You can manually download IPython from GitHub or PyPI. To install one of these versions, unpack it and run the following from the top-level source directory using the Terminal: pip install .


4 Answers

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

This is a great way of always being sure that the ipython instance always belongs to the virtualenv's python version.

This works only on ipython >2.0.

Source

like image 137
SiddharthaRT Avatar answered Oct 23 '22 08:10

SiddharthaRT


The answer given by @SiddharthaRT is good! Following this approach, it is simpler for me just:

python -m IPython

This will use the module IPython through the python bin, ensuring that it refers to the bin from the virtual env.

like image 35
jcozar87 Avatar answered Oct 23 '22 08:10

jcozar87


You can force IPython to use a virtual environment if available by adding file below to ~/.ipython/profile_default/startups:

import os
import sys

if 'VIRTUAL_ENV' in os.environ:
    py_version = sys.version_info[:2] # formatted as X.Y 
    py_infix = os.path.join('lib', ('python%d.%d' % py_version))
    virtual_site = os.path.join(os.environ.get('VIRTUAL_ENV'), py_infix, 'site-packages')
    dist_site = os.path.join('/usr', py_infix, 'dist-packages')

    # OPTIONAL: exclude debian-based system distributions sites
    sys.path = filter(lambda p: not p.startswith(dist_site), sys.path)

    # add virtualenv site
    sys.path.insert(0, virtual_site)

I recommend naming it 00-virtualenv.py so changes will be made as early as possible.

Note: Make sure ipython is installed in the new virtual environment to get this to work.

like image 10
rgtk Avatar answered Oct 23 '22 08:10

rgtk


  1. Activate your virtual environment by using source ~/.virtualenvs/my_venv/bin/activate or by running workon my_venv (Depending on how you've installed the my_venv virtual environment)

  2. Install ipython

pip install ipython

  1. Now run ipython from my_venv.

If it still loads the system's ipython, then run hash -r in your shell

This will reset the cache of your shell and ensure you don't run another ipython that was already called in this shell session.

like image 15
TheDataGuy Avatar answered Oct 23 '22 08:10

TheDataGuy