【问题标题】:Scripting virtualenvwrapper mkvirtualenv脚本 virtualenvwrapper mkvirtualenv
【发布时间】:2012-10-18 04:36:41
【问题描述】:

我正在 python 2.7 中编写game,并希望编写我的游戏开发环境的“引导程序”脚本,然后调用shovel。如果未检测到 virtualenvwrapper,我将使用virtualenv bootstrap solution。但是,如果检测到 virtualenvwrapper ,我想改用它。

问题是我的引导脚本没有继承 virtualenvwrapper 内联 shell 函数。据我所知,这排除了运行“mkvirtualenv NotOrion”之类的东西。由于设置了环境变量“VIRTUALENVWRAPPER_VIRTUALENV”(在我的情况下,来自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

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

/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”吗?如果没有,我可以从我的脚本中运行具有相同效果但产生警告的其他内容吗?

【问题讨论】:

  • 同样的问题,编写引导脚本。谢谢!

标签: bash virtualenvwrapper


【解决方案1】:

你的脚本应该可以做到:

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

我将它用于一些部署脚本。

【讨论】:

  • macports virtualenvwrapper 似乎没有提供“virtualenvwrapper.sh”。它还有其他变种,例如“virtualenvwrapper.sh-2.7”,所以我似乎不能指望这个。
  • 啊,是的。如果您使用 MacPorts,您会在处理它们所做的所有不受支持的更改时遇到困难。这个特别烦人,因为没有理由这样做 - 单个 virtualenv 安装可以处理许多不同版本的 Python(我有一个用于 Python 2.5、2.6、2.7、3.2、3.3、PyPy 和 Jython 的安装)
  • 这让我走上了完成我的引导脚本的轨道;我将把缺失的部分粘贴到上面并接受这个答案。
【解决方案2】:

似乎没有“标准”的方式来做到这一点。所以我手动查看了各种可能的地方。乱七八糟,但似乎是唯一的方法:

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
}

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    相关资源
    最近更新 更多