Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading a Python 3 virtual environment [duplicate]

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?

like image 944
Jonathan Avatar asked Jan 01 '16 17:01

Jonathan


People also ask

Can I upgrade Python version in virtualenv?

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.


1 Answers

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.

like image 184
jbg Avatar answered Oct 17 '22 13:10

jbg