2

我已经用 python2.6 在系统上安装了 virtualenv。

我将系统python升级到2.7,但是virtualenv对python2.6还是有亲和力的。

我尝试了easy_install --upgrade virtualenv,但这并没有改变任何东西。

有谁知道如何更新系统安装的 virtualenv 以在系统上使用新的 python2.7?

4

3 回答 3

3

pip、easy_install 和 virtualenv 命令根据python 版本(python 2.6、2.7 等)安装。

您必须easy_install为您的 python 版本安装一个新副本(请参阅setuptools 安装说明,或pip通过直接安装来执行相同操作。

然后,您可以使用这个与 python 2.7 绑定的新安装来安装virtualenv.

新命令很可能已安装为pip-2.7or easy_install-2.7; 请参阅setuptools有关多个 python 版本的文档pip,它们easy_install本身很可能是其 2.6 版本的符号链接。尝试运行pip-2.7 install virtualenveasy_install-2.7 virtualenv

如果这对您不起作用,您可以随时使用-m开关:

python2.7 -m easy_install virtualenv
于 2012-07-17T18:40:44.197 回答
1

您可以从虚拟环境中尝试pip install -U python,不确定它会破坏什么。

您还可以更改指向旧 Python 的符号链接,但不确定会产生什么副作用。

我会推荐最安全的路径,即首先pip freeze > installed.txt使用新的 Python 并pip install -r installed.txt在其中重新创建 virtualenv。

于 2012-07-17T18:12:42.707 回答
1

我创建了一个脚本来重新生成您的 virtualenv(s):https ://gist.github.com/WoLpH/fb98f7dc6ba6f05da2b8

如果您有很多要更新,您可以使用此脚本使用 GNU Parallel 并行化它:https ://gist.github.com/WoLpH/fb98f7dc6ba6f05da2b8#file-recreate_virtualenvs-sh

#!/bin/zsh -e

export PATH="/usr/local/bin:$PATH"
. $(which virtualenvwrapper.sh)

envs=$(find ~/envs -mindepth 1 -maxdepth 1 -type d -print -or -name '*.sparseimage' -print | sed -e 's/.*\///' | sed 's/.sparseimage$//' | sort -u)
echo "$envs" | parallel -v --no-notice ~/scripts/recreate_virtualenv.sh {}

只需将其复制到一个文件(上面的可下载链接)并像这样执行它:zsh -e recreate_virtualenvs.sh <project_name>

#!/bin/zsh -e

if [ ! -d "$PROJECT_HOME" ]; then
    echo 'Your $PROJECT_HOME needs to be defined'
    echo 'http://virtualenvwrapper.readthedocs.org/en/latest/install.html#location-of-project-directories'
    exit 1
fi

if [ "" = "$1" ]; then
    echo "Usage: $0 <project_name>"
    exit 1
fi

env="$1"
project_dir="$PROJECT_HOME/$1"
env_dir="$HOME/envs/$1"

function command_exists(){
    type $1 2>/dev/null | grep -vq ' not found'
}

if command_exists workon; then
    echo 'Getting virtualenvwrapper from environment'
    # Workon exists, nothing to do :)

elif [ -x ~/bin/mount_workon ]; then
    echo 'Using mount workon'
    # Optional support for packaged project directories and virtualenvs using
    # https://github.com/WoLpH/dotfiles/blob/master/bin/mount_workon
    . ~/bin/mount_workon
    mount_file "$project_dir"
    mount_file "$env_dir"

elif command_exists virtualenvwrapper.sh; then
    echo 'Using virtualenvwrapper'
    . $(which virtualenvwrapper.sh)
fi

if ! command_exists workon; then
    echo 'Virtualenvwrapper not found, please install it'
    exit 1
fi

rmvirtualenv $env || true

echo "Recreating $env"
mkvirtualenv $env || true
workon "$env" || true
pip install virtualenv{,wrapper}

cd $project_dir
setvirtualenvproject 

if [ -f setup.py ]; then
    echo "Installing local package"
    pip install -e .
fi

function install_requirements(){
    # Installing requirements from given file, if it exists
    if [ -f "$1" ]; then
        echo "Installing requirements from $1"
        pip install -r "$1"
    fi
}

install_requirements requirements_test.txt
install_requirements requirements-test.txt
install_requirements requirements.txt
install_requirements test_requirements.txt
install_requirements test-requirements.txt

if [ -d docs ]; then
    echo "Found docs, installing sphinx"
    pip install sphinx{,-pypi-upload} py
fi

echo "Installing ipython"
pip install ipython

if [ -f tox.ini ]; then
    deps=$(python -c "
parser=__import__('ConfigParser').ConfigParser();
parser.read('tox.ini');
print parser.get('testenv', 'deps').strip().replace('{toxinidir}/', '')")
    echo "Found deps from tox.ini: $deps"
    echo $deps | parallel -v --no-notice pip install {}
fi

if [ -f .travis.yml ]; then
    echo "Found deps from travis:"
    installs=$(grep 'pip install' .travis.yml | grep -v '\$' | sed -e 's/.*pip install/pip install/' | grep -v 'pip install . --use-mirrors' | sed -e 's/$/;/')
    echo $installs
    eval $installs
fi

deactivate
于 2014-11-04T13:15:04.283 回答