6

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?

Community
  • 1
  • 1
EdwardTeach
  • 615
  • 6
  • 18

2 Answers2

12

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.

clint
  • 14,402
  • 12
  • 70
  • 79
Chris Adams
  • 4,966
  • 1
  • 30
  • 28
  • It seems the macports virtualenvwrapper doesn't provide "virtualenvwrapper.sh". It has other variants such as "virtualenvwrapper.sh-2.7", so it seems I can't count on this. – EdwardTeach Oct 28 '12 at 21:41
  • Ah, yes. If you use MacPorts you're sort of stuck dealing with all of the unsupported changes they make. This one is particularly annoying since there's no reason for it - a single virtualenv install can handle many different versions of Python (I have a single install used for Python 2.5, 2.6, 2.7, 3.2, 3.3, PyPy and Jython) – Chris Adams Oct 30 '12 at 15:01
  • This got me on track for completing my bootstrap script; I'll paste the missing piece in above and accept this answer. – EdwardTeach Nov 15 '12 at 13:21
2

There doesn't appear to be a "standard" way to do this. So I manually looked in various likely places. Messy, but it appears to be the only way:

function find_virtualenvwrapper {
   # no consistent way to find 'virtualenvwrapper.sh', so try various methods
   # is it directly available in the path?
   virtualenvwrapper_path=$(which virtualenvwrapper.sh)
   if [ $? -eq 0 ]; then
      return
   fi
   # nope; how about something that looks like it in our path?
   # http://stackoverflow.com/questions/948008/linux-command-to-list-all-available-commands-and-aliases
   virtualenvwrapper_cmd=$(compgen -ac | grep -i 'virtualenvwrapper\.sh' | sort | uniq | head -1)
   if [ -n "$virtualenvwrapper_cmd" ]; then
      virtualenvwrapper_path=$(which $virtualenvwrapper_cmd)
      if [ $? -eq 0 ]; then
         return
      fi
   fi
   # still not; Debubuntu puts it in /etc/bash_completion.d
   virtualenvwrapper_path='/etc/bash_completion.d/virtualenvwrapper'
   if [ -e "$virtualenvwrapper_path" ]; then
      return
   fi
   # any other methods to find virtualenvwrapper can be added here
   echo "unable to find virtualenvwrapper.sh or anything that looks like it"
   exit 1
}
EdwardTeach
  • 615
  • 6
  • 18