6

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?

sunwarr10r
  • 4,420
  • 8
  • 54
  • 109
  • AFAIK it's not possible to update python inside a venv, for me the typical way is to create a new one, so option 2... – Antwane Oct 29 '18 at 08:04

2 Answers2

15

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.

Depending on the version you're upgrading from, it might be useful to know that Python 2 end of life was Jan 2020 and Python 3.5 was Sept 2020 (more details). If you have a choice (and if it matters) it's safer and more secure to use a version of Python that is still supported.

Nic
  • 1,518
  • 12
  • 26
0

I don't think it is possible to update an existing virtualenv (you'll find some way to do it on internet, but they basically create a new one / install a new python version and replace the version in the virtualenv).

I would rather create a new virtualenv.

Futhermore by conserving your current virtualenv you'll be able to rollback if you have any problem during the migration.

So :

  1. Create a new git branch : myproject-python37
  2. Create a new virtualenv
  3. Make the migration
PyNico
  • 695
  • 5
  • 21