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.

, , , ,

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

  1. Peter V May 27, 2014 at 6:57 am #

    Hello Becky, This is almost 3 years too late but I wanted clarification on
    “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

    I assume you don’t mean to add the github.com url to the bottom of ipython_config.py. I think the intent was to use one of the examples to make a virtualenv.py file and then add

    def main():
    execf(‘~/.ipython/virtualenv.py’)

    to the bottom of the configuration file. Correct?

    Thank you for the guide. It appears that few people are doing installation on Windows.

  2. Becky May 29, 2014 at 10:33 am #

    That github gist link goes to the code that you should add to the bottom of your ipython_config.py file.

    When I first put up these instructions, I was using a WP plug-in that displayed Github gist code in the body of the post. Clearly, that’s not working anymore.

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