0

I want to have a generic BASH script witch activates my virtual environment inside a given folder.

  • The script should be able to be called from from any folder I have a virtual environment in.
  • If there is no virtual environment it should create one and install the pip requirements.

I can not run the activation inside my original BASH only as a subprocess (see --rcfile). Just source-ing it is not working!

Thats my current script:

#!/bin/bash -e

# BASH script to run virtual environment

# Show errors
set -x

DIR_CURRENT="$PWD"
DIR_VIRTUAL_ENV="$PWD/venv"
FILE_PYTHON="/usr/bin/python2.7"
FILE_REQUIREMENTS="requirements.txt"
FILE_VIRTUAL_ACTIVATE_BASH="$DIR_VIRTUAL_ENV/bin/activate"

# CD to current folder
cd ${DIR_CURRENT}

echo DIR: $(pwd)

# Create the virtual environment if not existing
if [ ! -d ${DIR_VIRTUAL_ENV} ]; then
    virtualenv -p ${FILE_PYTHON} ${DIR_VIRTUAL_ENV}
    chmod a+x ${FILE_VIRTUAL_ACTIVATE_BASH}
    source ${FILE_VIRTUAL_ACTIVATE_BASH}
    pip install -r ${FILE_REQUIREMENTS}
fi

/bin/bash --rcfile "$FILE_VIRTUAL_ACTIVATE_BASH"

# Disable errors
set +x

I use Mac OSX 10.10.5 and Python 2.7.

Sadly existing 1, 2, 3 questions couldn't answer my problem.

Community
  • 1
  • 1
lony
  • 6,733
  • 11
  • 60
  • 92

1 Answers1

0

First, what you are trying to do has already been nicely solved by a project called virtualenvwrapper.


About your question: Use a function instead of a script. Place this into your bashrc for example:

function enter_venv(){

    # BASH script to run virtual environment

    # Show errors
    set -x

    DIR_CURRENT="$PWD"
    DIR_VIRTUAL_ENV="$PWD/venv"
    FILE_PYTHON="/usr/bin/python2.7"
    FILE_REQUIREMENTS="requirements.txt"
    FILE_VIRTUAL_ACTIVATE_BASH="$DIR_VIRTUAL_ENV/bin/activate"

    # CD to current folder
    cd ${DIR_CURRENT}

    echo DIR: $(pwd)

    # Create the virtual environment if not existing
    if [ ! -d ${DIR_VIRTUAL_ENV} ]; then
        virtualenv -p ${FILE_PYTHON} ${DIR_VIRTUAL_ENV}
        chmod a+x ${FILE_VIRTUAL_ACTIVATE_BASH}
        source ${FILE_VIRTUAL_ACTIVATE_BASH}
        pip install -r ${FILE_REQUIREMENTS}
    fi

    /bin/bash --rcfile "$FILE_VIRTUAL_ACTIVATE_BASH"

    # Disable errors
    set +x
}

The you can call it like this:

enter_venv
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thank you, as I understood it `virtualenvwrapper` is just creating environments globally? Is it also automatically building and activating them? The function you posted is sadly not running inside my "root" shell, still creating a new one. – lony Feb 22 '16 at 15:09
  • virtualenvwrapper can do what you want. Just read the docs (completely). To make my above example work you need to start a new terminal/shell. Otherwise your bashrc doesn't get's sourced again. (Please don't tell me you put the function in a script and executed it!) – hek2mgl Feb 22 '16 at 15:21
  • Is it possible for you to also post a virtualwrapper example? – lony Feb 24 '16 at 08:37
  • with virtualenvwrapper: `mkvirtualenv -ppython2.7 NAME`.. And from then `workon NAME`.. You need to manually execute `pip install -r requirements.txt`. Make sure that you installed virtualenvwrapper properly as described in the manual – hek2mgl Feb 24 '16 at 08:48
  • Thanks you. Is there something witch types workon automatically when I enter an "existing" workfolder? – lony Feb 24 '16 at 09:33
  • At the end you don't want that. – hek2mgl Feb 24 '16 at 10:03