Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 591
EdwardTeach Avatar asked Oct 28 '12 18:10

EdwardTeach


2 Answers

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.

like image 148
Chris Adams Avatar answered Sep 18 '22 13:09

Chris Adams


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
}
like image 24
EdwardTeach Avatar answered Sep 22 '22 13:09

EdwardTeach