I\'m thinking about putting the virtualenv for a Django web app I am making inside my git repository for the app. It seems like an easy way to keep deploy\'s simple and easy
I use what is basically David Sickmiller's answer with a little more automation. I create a (non-executable) file at the top level of my project named activate
with the following contents:
[ -n "$BASH_SOURCE" ] \
|| { echo 1>&2 "source (.) this with Bash."; exit 2; }
(
cd "$(dirname "$BASH_SOURCE")"
[ -d .build/virtualenv ] || {
virtualenv .build/virtualenv
. .build/virtualenv/bin/activate
pip install -r requirements.txt
}
)
. "$(dirname "$BASH_SOURCE")/.build/virtualenv/bin/activate"
(As per David's answer, this assumes you're doing a pip freeze > requirements.txt
to keep your list of requirements up to date.)
The above gives the general idea; the actual activate script (documentation) that I normally use is a bit more sophisticated, offering a -q
(quiet) option, using python
when python3
isn't available, etc.
This can then be sourced from any current working directory and will properly activate, first setting up the virtual environment if necessary. My top-level test script usually has code along these lines so that it can be run without the developer having to activate first:
cd "$(dirname "$0")"
[[ $VIRTUAL_ENV = $(pwd -P) ]] || . ./activate
Sourcing ./activate
, not activate
, is important here because the latter will find any other activate
in your path before it will find the one in the current directory.