Virtualenv Bash Shell Function

By default, virtualenv uses /usr/bin/python. I find this a pain because I run the latest Python out of /usr/local/bin. Each time I create a new virtualenv environment, I need to type:

virtualenv -p /usr/local/bin/python {env}

If you use Bash, create the following shell function to save precious keystrokes or guard against creating your virtualenv with the ancient python interpreter included as the system default:

function virtualenv()
{
/usr/local/bin/virtualenv -p /usr/local/bin/python "$@"
}

Simply place the above in your Bash config file. This is your .bashrc, .bash_profile, or .bash_login file, depending on your Unix variant platform.

(To figure out which one, see here: http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html)

By the way, when you create a shell function with the same name as the shell program/script, make sure you have the full path of the program specified in the shell function. IF you don’t, the shell will resolve the command back to the shell function. And you are stuck in an infinite recursive loop (turtles all the way down…)

Advertisements
Tagged with: , ,
Posted in Computing, Programming, Python