Tuesday, June 29, 2010

SciPy 2010: Basic Tutorials -- IPython and virtualenv

The basic tutorials at SciPy were well-taught and allowed someone unfamiliar with numpy, scipy, and ipython (like me!) to get up to speed and productive very quickly. I now feel confident with the new tools in my python toolbox. Thanks!

I use virtualenv extensively to control my python site-packages environment. This allows me to experiment with different versions of the python packages I use, including repository sources, without polluting or corrupting my system's overall python environment. It's a great tool and easy to use, at least in the Red Hat and Debian-based Linux systems I use daily.

However, one thing I discovered was that ipython unfortunately did not work properly in my virtualenv'ed python environments. Regardless if I had activated a virtualenv environment, ipython always used the system site packages only.

In order for virtualenv to do it's magic in creating a virtual python environment, it relocates python (so that the python path, etc. are set correctly). For example, if there is a virtualenv in the directory "~/dev/pyenv", "which python" will return "~/dev/pyenv/bin/python" if that virtualenv is active.

The problem is that the shebang in the ipython script (/usr/bin/ipython on my system) points to the system python, not the virtualenv one. So whenever ipython is executed, it will use the system python, not the virtualenv one.

There are a number of ways to address this. One could "easy_install ipython" in every virtualenv where ipython is desired. This seems excessive, and redundant in virtualenv's where the system site packages are included. Note, you'll still need to "pip install ipython" in virtualenv's without the system site packages included (--no-site-packages option) as that creates a clean and empty python environment with nothing previously installed.

One could instead edit the ipython script to shebang "python", removing the absolute path. Then, when ipython is executed, it will use whatever python command is in the path. But if ipython is upgraded, that edit could be lost. It also directly changes a system package, which is generally not preferred.

Therefore, the best solution is to is just bypass the shebang by running "python /usr/bin/ipython" rather than just "ipython". This way, the shebang will be ignored, and the path's python will get run. This will be the system python if not in a virtualenv environment, or the virtualenv's python if active. However, remembering to type "python /usr/bin/ipython" instead of "ipython" is a pain. An alias in .bash_aliases like "alias ipython='python /usr/bin/ipython'" is the answer here.

Hopefully this helps others who want to use ipython in their virtualenv environments. I am looking forward to using these new tools and hear all the cool things people are using python for at the conference proper tomorrow and Thursday.

With this, ipython will work perfectly in both system and virtualenv environments.

No comments:

Post a Comment