4

Each time I open terminal I have to run this command

source $(which virtualenvwrapper.sh)

To use

workon myenv

I would like to know what I need to add to .bashrc so I could use workon command instantly, without using source before

I'm using Ubuntu 14.04

micgeronimo
  • 2,069
  • 5
  • 23
  • 44

3 Answers3

11

As per the virtualenvwrapper docs, you can add the following to .bashrc:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

I personally like the lazy loading option as it keeps shell startup speed fast.

keybits
  • 4,383
  • 2
  • 20
  • 18
  • N.b. `.sh` file [sometimes gets put elsewhere](https://stackoverflow.com/a/16705807/1461850) (e.g. with `pip install`) – Lee Sep 13 '17 at 22:11
2

I have this in my .bash_profile to help with moving between virtual environments. It includes the info needed to not have to source the shell script every time, but it also includes a way to automatically 'workon' an environment when you cd into the directory.

I'm not sure I see the point of virtualenvs if you only have one.

export WORKON_HOME=~/.virtualenvs
export PROJECT_HOME=~/Development/python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

# Call virtualenvwrapper's "workon" if .venv exists.
# Source: https://gist.github.com/clneagu/7990272
# This is modified from--
# https://gist.github.com/cjerdonek/7583644, modified from
# http://justinlilly.com/python/virtualenv_wrapper_helper.html, linked from
# http://virtualenvwrapper.readthedocs.org/en/latest/tips.html
#automatically-run-workon-when-entering-a-directory
check_virtualenv() {
    if [ -e .venv ]; then
        env=`cat .venv`
        if [ "$env" != "${VIRTUAL_ENV##*/}" ]; then
            echo "Found .venv in directory. Calling: workon ${env}"
            workon $env
        fi
    fi
}
venv_cd () {
    builtin cd "$@" && check_virtualenv; ls -FGlAhp
}
# Call check_virtualenv in case opening directly into a directory (e.g
# when opening a new tab in Terminal.app).
check_virtualenv
alias cd="venv_cd"
Todd Vanyo
  • 545
  • 5
  • 15
2

I use this in my .bashrc file to automatically workon the most recently activated virtual environment.

if [ -f  /usr/local/bin/virtualenvwrapper.sh ]; then
  source /usr/local/bin/virtualenvwrapper.sh

  # Set up hooks to automatically enter last virtual env
  export LAST_VENV_FILE=${WORKON_HOME}/.last_virtual_env
  echo -e "#!/bin/bash\necho \$1 > $LAST_VENV_FILE" > $WORKON_HOME/preactivate
  echo -e "#!/bin/bash\necho '' > $LAST_VENV_FILE" > $WORKON_HOME/predeactivate
  chmod +x $WORKON_HOME/preactivate
  chmod +x $WORKON_HOME/predeactivate
  if [ -f  $LAST_VENV_FILE ]; then
    LAST_VENV=$(tail -n 1 $LAST_VENV_FILE)
    if [ ! -z $LAST_VENV ]; then
      # Automatically re-enter virtual environment
      workon $LAST_VENV
    fi
  fi
fi

The preactivate and deactivate hooks are modified so that when a virtual environment is activated, the name of the virtual environment is dumped to a file, and when it's deactivated the contents of the file is erased. This is similar to @Todd Vanyo's answer, but works when activating/deactivating instead of when navigating directories.

cs01
  • 5,287
  • 1
  • 29
  • 28