Monday, April 30, 2012

virtualenvwrapper on Ubuntu 12.04

If you use Ubuntu, you were probably pretty excited when you turned on your machine this week and a window popped up asking if you'd like to upgrade to Precise Pangolin.

Of course there are many new things to explore. After I upgraded, one thing I noticed, is that virtualenvwrapper.sh is not in /usr/bin or anywhere in /usr/lib/python2.7 actually it's in /etc/bash_completion.d/virtualenvwrapper. It seems as if this script has actually bounced around a fair bit. In Ubuntu 9.04 it was in /usr/local/bin/virtualenvwrapper_bashrc. And of course it depends on how you installed it. If you used pip then it will install in /usr/local/bin (also see this), as if you were on a generic Linux (non-Debian) box.

Update (2012-05-01): See the source on Launchpad for yourself:

In contrast to the information in
/usr/share/doc/virtualenvwrapper/en/html/index.html this package installs
virtualenvwrapper.sh as /etc/bash_completion.d/virtualenvwrapper.
Virtualenvwrapper is enabled if you install the package bash-completion and
enable bash completion support in /etc/bash.bashrc or your ~/.bashrc.
If you only want to use virtualenvwrapper you may just add
 source /etc/bash_completion.d/virtualenvwrapper
to your ~/.bashrc.

One of the implications of this is that the first time you start your terminal after instlalling virtualenvwrapper, it will run the virtualenvwrapper shell script in your bash completion directory. If you have not already set up a $WORKON directory, it will automatically set this to ~/.virtualenvs. It doesn't set the $PROJECT_HOME variable though. You can still add export WORKON_HOME=/path/to/your/virtualenvs to your .bashrc, .profile, .bash_profile, etc. (whichever applies to you). But it is obviously not necessary to add source /path/to/virtualenvwrapper.sh, since this file doesn't exist! And the virtualenvwrapper shell script will automatically run every time you start your terminal already because it is in the bash completion folder which is called indirectly from your global bashrc file (might be bash.bashrc or something else). file in /etc. Because .virtualenvs starts with a dot, Ubuntu will probably hide it so use control-H to reveal hidden folders and files in your file browser (probably Nautilus) or la (ls -A) in the terminal.

The upshot of this, is unless you want to set a project folder for virtualenvwrapper, you can start using it the instantly without any setup or murky .bashrc edits. Enjoy!

2 comments:

  1. Jesus.

    Thanks for the information dump... I hate figuring out where the hell this script ends up. Drives me nuts. The problem with the bash_completion.d/ route is that tools like fabric don't automatically understand the "workon" command (since it's not actually an executable command on the filesystem).

    At least in that situation, fabric just needs to source the completion file:

    # Indentation is probably busted
    with prefix("source /etc/bash_completion.d/virtualenvwrapper"):
    with prefix("workon myenv"):
    run("command")

    ReplyDelete
  2. Well this explains why non-user accounts like postgres magically wind up with .virtualenvs-dirs. WTF, ubuntu, but I guess I should be used to it by now.

    ReplyDelete

Fork me on GitHub