Making iPython Virtualenv Aware

iPython is a great replace­ment for the stan­dard Python shell, but, out of the box, it doesn’t play well with virtualenv.

This can be fixed with a cou­ple of changes:

In the ter­mi­nal gen­er­ate an iPython default con­fig file:

ipython profile create

Open the newly gen­er­ated file:

vi ~/.ipython/profile_default/ipython_config.py

Add the fol­low­ing 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 vir­tualenv aware.

4 September, 2012 — 2 comments

Installing PIL on the Mac

The Python Imag­ing Library, PIL, is a very use­ful addi­tion to your Python instal­la­tion if you need to manip­u­late pic­tures. Django uses it to val­i­date upload images for example.

On a Mac you do need to install lib­jpeg beforehand.

Install lib­jpeg:

  • Get the source: http://www.ijg.org/files/jpegsrc.v6b.tar.gz

  • Extract the archive.

  • Move inside the source direc­tory and exe­cute the fol­low­ing commands:

    cp /usr/share/libtool/config.sub .
    cp /usr/share/libtool/config.guess .
    ./configure --enable-shared
    make
    sudo mkdir -p /usr/local/include
    sudo mkdir -p /usr/local/bin
    sudo mkdir -p /usr/local/lib
    sudo mkdir -p /usr/local/man/man1
    sudo make install
    

Install PIL:

  • Get PIL at: http://effbot.org/downloads/Imaging-1.1.6.tar.gz

  • Extract the archive.

  • Move inside the source directory

  • Change the fol­low­ing val­ues in setup.py from the default None to:

    JPEG_ROOT = "/usr/local/include"
    ZLIB_ROOT = "/usr/local/include"
    
  • Check if every­thing is well configured:

    python setup.py build_ext -i
    python selftest.py
    
  • If no errors are found and the required libraries are installed (like JPEG sup­port), install PIL:

    sudo python setup.py install
    

And that’s it, I’ve tried this on Mac OS Leop­ard, and it works fine, let me know how it went for you.

14 March, 2008 — 3 comments