Forums

Update python version in virtualenv

Dumb question, but is the best way to update my python version (3.4 --> 3.6) in a virtual environment to pip freeze the requirements, delete the old virtual environment folder, create a new one with the new python version, and install from the requirements.txt?

If so, is it possible to create a new virtualenv with the same name as the old one (hopefully that would allow me not to have to change anything else in my web app settings)?

yes, with one extra step- you will have to also change the webapp python version in the webapp tab. (also some packages might be supported in 3.4 but not in 3.6 etc, so you might have to debug and fix that)

I guess you could keep it the same name (but I believe the only thing you need to change is the virtualenv path in the webapps tab and then reload the webapp, so shouldn't be a lot of changes even if you use a different name)

I did the above (I needed to go from python 2.7 to 3.7) and I created the new env with the following from https://stackoverflow.com/questions/10218946/upgrade-python-in-a-virtualenv

Step 1: Freeze requirement & take a back-up of existing env

pip freeze > requirements.txt
deactivate
mv env env_old

Step 2: Install Python 3.7 & activate virutal environment

sudo apt-get install python3.7-venv
python3.7 -m venv env
source env/bin/activate
python --version

Step 3: Install requirements

sudo apt-get install python3.7-dev
pip3 install -r requirements.txt

The problem at this point is that when I restart the webapp and reload my page, I get an error message saying that it can't find django. (I then reinstalled django which of course killed everything so I restored my old env to get back to the starting point.)

Make sure that your web app is actually configured to use the virtualenv that you're installing into and make sure that django is in your requirements file.