Python, Django, & MySQL on Windows 7, Part 3: iPython & Virtual Environments

This is the third post in a dummies guide to getting started with Python, Django, & MySQL on Windows 7.

The last installment covered setting up virtual environments.

Part 3: iPython and Virtual Environments

iPython is not virtual environment-aware

iPython is a valuable tool, but you’ll have to tweak it to work with virtual environments. By default, iPython isn’t aware of a virtual environment’s packages.

For example, if you install a package into a virtualenv and try to use it via Python’s built-in shell, everything works:

import via python

Try the same thing with iPython, however, and you get an import error:

import via ipython

A little intervention is required to make iPython virtual environment-aware. If you’re okay with using Python’s built-in interactive shell instead of iPython, skip ahead to Part 4: Installing Django.

Install iPython to the virtual environment

The easiest and most obvious solution to make iPython work with a virtual environment is to install it to the virtual environment.

However, you will have to do this for each environment you work in.  Furthermore, if you created the virtual environment without the –no-site-packages option (which tells virtualenv not to inherit anything from global site-packages), you may get an “already installed” message.

Modify iPython config file

Another way to ensure iPython behaves with virtual environments is to use its configuration file to check for an active virtual environment and modify the import path accordingly. The directions below assume iPython is installed globally but not in any of your virtual environments.

    1. Open a command window.
    2. Instruct iPython to generate a sample configuration file (called ipython_config.py) by typing ipython profile create
    3. The sample file should now be in your iPython profile folder. On Windows, this is [your user folder]\.ipython\profile_default.
    4. Open ipython_config.py in a text editor and add the following code to the bottom. I relied heavily on code from here and here, making a few tweaks.

      https://gist.github.com/1176035

    5. Activate a virtual environment.
    6. Start up iPython and look for the output confirming the current active virtual environment:ipython & virtualenv
The next installment is Part 4: Installing Django.

, , , ,

Trackbacks/Pingbacks

  1. Python, Django, & MySQL on Windows 7, Part 2: Virtual Environments | Computers are for People - August 28, 2011

    [...] Part 3: making iPython work in virtual environments [...]

  2. Python, Django, & MySQL on Windows 7, Part 1: Getting Started | Computers are for People - September 14, 2011

    [...] Part 3: making iPython work in virtual environments [...]

  3. Python, Django, & MySQL on Windows 7, Part 4: Installing Django | Computers are for People - September 14, 2011

    [...] Part 3: making iPython recognize virtual environments [...]

  4. Python, Django, MySQL & Win 7 | Computers are for People - September 15, 2011

    [...] Part 3: making iPython recognize virtual environments [...]

  5. Python, Django, & MySQL on Windows 7, Part 5: Installing MySQL | Computers are for People - September 29, 2011

    [...] Part 3: making iPython recognize virtual environments [...]

Leave a Reply