rbenv and virtualenv performance tip: load on demand

I found that new terminal sessions were taking a while to load, so I started commenting things out in my bash profile (How to be a Programmer: A Short, Comprehensive, and Personal Summary calls it a Binary Search). I found that it was snappy when I commented out my calls to virtualenvwrapper and rbenv.

Of course, with these calls I lost the ability to use them, and I lost the defaults. To fix this I chose a faster way to load the defaults and made functions for loading the full functionality of these. Here is the relevant part of ~/.bash_profile.

# Python
# my virtualenv is called "batv", replace this with what you passed to mkvirtualenv
source $HOME/.virtualenvs/batv/bin/activate
loadvirtualenv() {
  # the next two lines might not be necessary for you; I only have them on OS X and not Linux
  export PYTHONPATH=/Library/Python/2.7/site-packages/virtualenv-1.6.4-py2.7.egg
  export PYTHONPATH=/Library/Python/2.7/site-packages/virtualenvwrapper-2.10.1-py2.7.egg:$PYTHONPATH
  source /usr/local/bin/virtualenvwrapper.sh
  workon batv

# Ruby
export PATH="$HOME/.rbenv/shims/:$HOME/.rbenv/bin:$PATH"
loadrbenv() {
  eval "$(rbenv init -)"

To use my default virtualenv or to use rbenv without the rbenv shell command, I don’t have to run either of these functions. If I want to do advanced stuff with virtualenvwrapper, I type loadvirtualenv and hit enter, and then I can use all of virualenvwrapper in that terminal session. If I want to set a rbenv environment for my current shell, I type loadrbenv.

Before I made these changes, the contents each of these functions were run each time I created a terminal. Now they’re run only on demand and opening a new terminal tab is much snappier!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.