Is there a way to upgrade the version of python used in a virtualenv (e.g. if a bugfix release comes out)?
I could pip freeze --local > requirements.txt
, then remove the directory and pip install -r requirements.txt
, but this requires a lot of reinstallation of large libraries, for instance, numpy
, which I use a lot.
I can see this is an advantage when upgrading from, e.g., 2.6 -> 2.7, but what about 2.7.x -> 2.7.y?
If you're using
pipenv
, I don't know if it's possible to upgrade an environment in place, but at least for minor version upgrades it seems to be smart enough not to rebuild packages from scratch when it creates a new environment. E.g., from 3.6.4 to 3.6.5:Updated again: The following method might not work in newer versions of virtualenv. Before you try to make modifications to the old virtualenv, you should save the dependencies in a requirement file (
pip freeze > requirements.txt
) and make a backup of it somewhere else. If anything goes wrong, you can still create a new virtualenv and install the old dependencies in it (pip install -r requirements.txt
).Updated: I changed the answer 5 months after I originally answered. The following method is more convenient and robust.
Side effect: it also fixes the
Symbol not found: _SSLv2_method
exception when you doimport ssl
in a virtual environment after upgrading Python to v2.7.8.Notice: Currently, this is for Python 2.7.x only.
If you're using Homebrew Python on OS X, first
deactivate
all virtualenv, then upgrade Python:Run the following commands (
<EXISTING_ENV_PATH>
is path of your virtual environment):Finally, re-create your virtual environment:
By doing so, old Python core files and standard libraries (plus
setuptools
andpip
) are removed, while the custom libraries installed insite-packages
are preserved and working, as soon as they are in pure Python. Binary libraries may or may not need to be reinstalled to function properly.This worked for me on 5 virtual environments with Django installed.
BTW, if
./manage.py compilemessages
is not working afterwards, try this:If you happen to be using the venv module that comes with Python 3.3+, it supports an
--upgrade
option. Per the docs:I moved my home directory from one mac to another (Mountain Lion to Yosemite) and didn't realize about the broken virtualenv until I lost hold of the old laptop. I had the virtualenv point to Python 2.7 installed by
brew
and since Yosemite came with Python 2.7, I wanted to update my virtualenv to the system python. When I ranvirtualenv
on top of the existing directory, I was gettingOSError: [Errno 17] File exists: '/Users/hdara/bin/python2.7/lib/python2.7/config'
error. By trial and error, I worked around this issue by removing a few links and fixing up a few more manually. This is what I finally did (similar to what @Rockalite did, but simpler):After this, I was able to just run virtualenv on top of the existing directory.