This page looks best with JavaScript enabled

Upgrade Python Version in a virtualenv

 ·  ☕ 2 min read

Background

I use virtualenv (actually virtualenvwrapper) for several python projects (nikola websites, django sites, and pure python projects). One of my virtualenv environments was created as a Python 2.7 environment and I needed to upgrade it to Python 3.x.

To upgrade a virtualenv should be simple as its “virtual”! Most articles I found said the easiest approach was to create a new virtualenv, freeze the old env, copy files from old environment to new and then pip install from freeze; however, I thought it should be easier.

Details

My approach is simple: create a new virtualenv over the top of the existing virtualenv. To do so:

  • assuming your virtualenv name is “projectX” and you are upgrading to python3.4
  • go to the parent directory of your virtualenv directory
  • make sure your virtualenv is not active (e.g. deactivate)
  • now is a good time to backup your project: tar -cjf projectX.tar.bz2 projectX/)
  • upgrade / overlay your virtualenv with a “new” virtualenv with the version of pyton you need: mkproject --force --python=/usr/bin/python3.4 projectX
  • if for some reason you did not deactivate prior to overlaying your virtualenv you will need to deactivate and then activate before your environment will see the new python version

Notes

  • I received a Python.h No such file or directory error complaining about gcc - the issue was I did not have the dev files installed for python3, (install with: apt-get install python3-dev)
Share on

drad
WRITTEN BY
drad
Sr. Consultant