2

I had installed virtualenv on the system with python2.6.

I upgraded the system python to 2.7, but the virtualenv still has affinity for python2.6.

I tried easy_install --upgrade virtualenv, but that didn't change anything.

Does anyone know how to update the system installed virtualenv to use the new python2.7 on the system?

littleidea
  • 437
  • 4
  • 5

3 Answers3

3

The pip, easy_install and virtualenv commands are installed per python version (python 2.6, 2.7, etc).

You'll have to install a new copy of easy_install for your python version (see the setuptools installation instructions, or do the same for pip by installing it directly.

You can then use this new installation, tied to python 2.7, to install virtualenv.

The new commands are likely, to have been installed as pip-2.7 or easy_install-2.7; see the setuptools documentation on multiple python versions, pip and easy_install themselves are likely to be a symlinks to their 2.6 versions. Try and run pip-2.7 install virtualenv or easy_install-2.7 virtualenv.

If that doesn't work for you, you can always use the -m switch instead:

python2.7 -m easy_install virtualenv
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

You could try pip install -U python from within the virtual environment, not sure what it would break.

You could also change the symlinks that point to the old Python, but not sure what side effects that would have.

I would recommend the safest path, that is to first pip freeze > installed.txt and then recreate your virtualenv with your new Python and pip install -r installed.txt in it.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

I have created a script to regenerate your virtualenv(s): https://gist.github.com/WoLpH/fb98f7dc6ba6f05da2b8

If you have many to update you can use this script to parallelize it using 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 {}

Simply copy it to a file (downloadable link above) and execute it like this: 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
Wolph
  • 78,177
  • 11
  • 137
  • 148