Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit .bashrc to use workon command of virtualenvwrapper

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

like image 272
micgeronimo Avatar asked Dec 05 '22 23:12

micgeronimo


2 Answers

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.

like image 118
keybits Avatar answered Dec 10 '22 13:12

keybits


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"
like image 39
Todd Vanyo Avatar answered Dec 10 '22 12:12

Todd Vanyo