脚本编写Virtualenvwrapper Mkvirtualenv



我正在用Python 2.7写游戏,并想脚本脚本录制游戏开发环境的" bootstrap",然后调用铲子。如果未检测到VirtualenvWrapper,我将使用Virtualenv Bootstrap解决方案。但是,如果已检测到VirtualenvWrapper ,我想使用它。

问题在于,我的Bootstrap脚本不会继承VirtualenvWrapper inline shell功能。据我所知,这排除了运行" Mkvirtualenv Notorion"之类的东西。由于环境变量" virtualenvwrapper_virtualenv" is set(在我的情况下,来自Macports:/opt/local/bin/virtualenv-2.7),我尝试直接使用它:

#!/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

我的引导脚本完成安装铲子。但是跑步铲(例如最后一行)会产生警告:

/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

那么,可以从脚本中以某种方式调用" mkvirtualenv"吗?如果没有,我可以从脚本中运行具有相同效果的其他内容,但是 产生警告?

您的脚本应该能够做:

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

我将其用于某些部署脚本。

似乎没有一种"标准"方法来执行此操作。因此,我在各个可能的地方手动看。凌乱,但这似乎是唯一的方法:

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
}

相关内容

  • 没有找到相关文章

最新更新