This is the third post in a dummies guide to getting started with Python, Django, & MySQL on Windows 7.
- Part 1: installing Python, pip, and iPython
- Part 2: virtual environments
- Part 3: making iPython work in virtual environments (you are here)
- Part 4: installing Django
- Part 5: installing MySQL
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:
Try the same thing with iPython, however, and you get an import error:
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.
- Open a command window.
- Instruct iPython to generate a sample configuration file (called ipython_config.py) by typing
ipython profile create
- The sample file should now be in your iPython profile folder. On Windows, this is [your user folder]\.ipython\profile_default.
- 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 - Activate a virtual environment.
- Start up iPython and look for the output confirming the current active virtual environment: