Is it bad to have my virtualenv directory inside my git repository?

前端 未结 9 2101
醉话见心
醉话见心 2020-12-04 04:42

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

9条回答
  •  北海茫月
    2020-12-04 05:11

    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.

提交回复
热议问题