2012-10-28 29 views
5

我在Python 2.7中编写了game,并且想要编写我的游戏开发环境的“bootstrap”,然后调用shovel。如果未检测到virtualenvwrapper,我将使用virtualenv bootstrap solution。但是,如果virtualenvwrapper 检测到,我想用它来代替。脚本virtualenvwrapper mkvirtualenv

问题是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 

我的启动脚本完成安装铲。但是运行铲(如最后一行)生成警告:

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

+0

相同的确切问题,写一个引导脚本。谢谢! – cloudrave

回答

11

你的脚本应该能够做到:

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

我使用一些部署脚本。

+0

似乎macports virtualenvwrapper不提供“virtualenvwrapper.sh”。它有其他的变种,比如“virtualenvwrapper.sh-2.7”,所以看起来我不能指望这一点。 – EdwardTeach

+0

啊,是的。如果你使用MacPorts,你会遇到困难,处理所有不支持的更改。这一个特别恼人,因为没有理由 - 一个virtualenv安装可以处理许多不同版本的Python(我有一个用于Python 2.5,2.6,2.7,3.2,3.3,PyPy和Jython的安装) –

+0

这让我在完成我的引导脚本的轨道上;我会在上面粘贴缺失的部分并接受此答案。 – EdwardTeach

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 
} 
相关问题