Search code examples
bashvirtualenvwrapper

Scripting virtualenvwrapper mkvirtualenv


I'm writing a game in python 2.7, and want to script the "bootstrap" of my game's development environment, and then invoke shovel. If virtualenvwrapper is not detected, I will use a virtualenv bootstrap solution. However if virtualenvwrapper is detected, I would like to use it instead.

The problem is that the virtualenvwrapper inline shell functions are not inherited by my bootstrap script. As far as I know, that rules out running something like "mkvirtualenv NotOrion". Since the environment variable "VIRTUALENVWRAPPER_VIRTUALENV" is set (in my case, from macports: /opt/local/bin/virtualenv-2.7), I tried using it directly instead:

#!/usr/bin/env bash

# Name your first "bootstrap" environment:
ENV_NAME=NotOrion
# Options for your first environment:
ENV_OPTS='--no-site-packages --distribute'

unset PYTHONDONTWRITEBYTECODE

function create_virtualenvwrapper_venv {
  echo "installing into virtualenvwrapper directory"
  cd $WORKON_HOME
  $VIRTUALENVWRAPPER_VIRTUALENV $ENV_OPTS $ENV_NAME
  cd -
  #mkvirtualenv $ENV_NAME
  #workon $ENV_NAME
}

function create_standalone_venv {
  # not run/snipped
}

if [ -z "$VIRTUALENVWRAPPER_VIRTUALENV" ]; then
  create_standalone_venv
else
  create_virtualenvwrapper_venv
fi

pip install shovel
shovel help

My bootstrap script finishes installing shovel. However running shovel (eg the last line) produces warnings:

/Users/me/.virtualenvs/NotOrion/bin/shovel:25: UserWarning: Module argparse was already imported from /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.pyc, but /Users/me/.virtualenvs/NotOrion/lib/python2.7/site-packages is being added to sys.path
import pkg_resources
# normal shovel output snipped

So is it possible to somehow invoke "mkvirtualenv" from a script? If not, can I run something else from my script that has the same effect but doesn't produce warnings?


Solution

  • Your script should be able to do:

    # 'which' will print absolute path to virtualenvwrapper.sh
    source `which virtualenvwrapper.sh`
    

    I use that for some deployment scripts.