Upgrading Python to 3.7 inside venv?

sunwarrior picture sunwarrior · Oct 29, 2018 · Viewed 10.6k times · Source

How can I upgrade the current Python interpreter inside a venv to v3.7.1. Unfortunately 3.5.2 is out of date for some libraries I use, so I want to switch to 3.7.1.

Option 1: is to update the interpreter inside my venv.

Option 2: is to create a new venv with Python 3.7.1 as its interpreter and deploy the whole project with all its dependencies and tweaks anew?

What is the typical way to port a Flask application to a newer interpreter?

Answer

Nick picture Nick · Oct 29, 2018

Much the easiest way is to create a new venv.

If you don't have a requirements.txt file in your app, now's the time to generate one and commit it into your version control software (Git, Mercurial etc). With the old venv activated:

>>> pip freeze >requirements.txt

Create the new venv with the desired python version and give it a name:

>>> virtualenv -p python3.7 venvname

Activate the venv:

>>> source venvname/bin/activate

Then install your requirements:

>>> pip install -r requirements.txt

should set the new venv up exactly as the old one was, give or take the odd version conflict. Fix those conflicts and rerun pip install -r until there are no more errors.

It's worth testing against this new temporary venv until you're sure enough to delete the original and recreate it on Py3.7.

In general if you're interested in renaming the venv, there are further details in this question but it's not advised.