I have installed Python 3.5.1 on top of 3.5.0 and I now want to update a virtual environment to use 3.5.1 but I can find no easy way to to this. It looks as if I will have to delete the virtual environment and then rebuild it with the new version of Python. Does anyone have a simpler way of doing this?
You can't upgrade to a Python version you don't already have on your system somewhere, so make sure to get the version you want, first, then make all the venvs you want from it. Save this answer.
For a minor upgrade (3.5.0 -> 3.5.1 or more generally, where only z
in x.y.z
is changing), you should not need to do anything.
The virtualenv, in its bin
subdirectory, has a symlink to the system Python executable like so:
python -> python3.5
python3 -> python3.5
python3.5 -> /usr/bin/python3.5
Since /usr/bin/python3.5
is replaced when you upgrade from 3.5.0 to 3.5.1, the virtualenv will automatically use the new Python version.
If you’re doing a more major upgrade (x
or y
in x.y.z
), you’ll need to upgrade the virtualenv.
If you’re using the built-in pyvenv
command (introduced in Python 3.3), it has an --upgrade
flag:
Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.
… which should do the trick. Note that the pyvenv
command is being replaced with python3 -m venv
in Python 3.6.
If you’re using the virtualenv
package rather than the built-in command, the most straightforward way to do this is to delete the virtualenv and recreate it with the new version of Python, and then run pip install -r requirements.txt
.
This assumes you have a requirements.txt
file for your project. You can create one of these files, which lists all the packages installed in your virtualenv, by running pip freeze --local > requirements.txt
before upgrading Python and recreating the virtualenv.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With