6

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?

Jonathan
  • 2,635
  • 3
  • 30
  • 49
  • 2
    A minor update like that (z in version x.y.z) should not require doing anything -- the virtualenv contains a symlink to the system python3.5 executable which will have been replaced. For bigger updates (x or y in version x.y.z) I usually just delete the environment, recreate it for the new version and then `pip install -r requirements.txt` – jbg Jan 01 '16 at 17:23
  • As the previous change, binary for python will be updated for minor versions. You can recreate it if you are not sure you can user the -p flag to select your python when creating a new virtualenv. – cdvv7788 Jan 01 '16 at 17:28

1 Answers1

4

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.

jbg
  • 4,903
  • 1
  • 27
  • 30
  • This did the trick nicely, thank you! – Jonathan Jan 02 '16 at 03:32
  • I do not have either a `requirements.txt` file or a time machine to let me create one before doing the upgrade. Do you know if there’s any better option than downgrading, getting the requirements file, and re-upgrading? – Daniel H Jan 09 '18 at 19:56