What is virtualenv
It's pretty typical to do Python coding within a virtual environment. The virtual environment isolates any Python packages/dependencies to that environment so they do not impact your system level packages or packages in another virtual environment. It solves the common dilemma of maintaining dependencies when “Project A depends on version 1.x but Project B depends on version 2.x”. You can set up a different virtual environment for each of your Python projects, each with different package dependencies or different versions of the same packages. Or you can set up a new virtual environment to test how your project will work with different package versions. virtualenv is a tool you use to create these isolated Python environments. virtualenvwrapper is a set of extensions for virtualenv designed to simplify working with virtual environments.
The problem with updating Python in a virtualenv
A glaring issue with
virtualenv is that although it is quite simple to install/remove/update or otherwise maintain any Python packages installed in the virtual environment,
virtualenv provides no built in way to upgrade or change the version of the Python interpreter installed in the virtual environment.
The only solution I am currently aware of is to manually create an entirely new virtual environment containing the desired Python interpreter. I know it sounds like a lot of work, but it is actually quite simple using the following procedure.
1. Freeze currently installed packages Save the current list of packages that are installed in the virtual environment you are updating.
pip freeze > requirements.txt
2. Commit any pending changes
You will be removing the existing virtual environment so make sure any pending updates have been committed to your version control system. This includes the dependencies just saved to
Alternatively you can make a copy of your virtual environment directories before proceeding and copy the relevant directories to the new virtual environment once it has been created. I will not be detailing that approach in this post.
3. Remove your existing virtual environment
Use different commands for
deactivate a virtual environment before removing it.
# if only virtualenv deactivate rm -r /path/to/virtual/environment
# if using virtualenvwrapper deactivate rmvirtualenv <environment name>
4. Create your new virtual environment
Create the new virtual environment with the desired Python interpreter (e.g.
# virtualenv virtualenv --python=PYTHON_EXE /path/to/new/virtual/environment
# virtualenvwrapper mkvirtualenv --python=PYTHON_EXE <environment name>
5. Activate the new virtual environment The normal command to activate the virtual environment.
# virtualenv source /path/to/virtual/environment/bin/activate
# virtualenvwrapper workon <environment name>
6. Restore development environment Clone repo from version control system to new virtual environment.
7. Install the dependencies
Reinstall dependencies that were recorded in
pip install -r requirements.txt