For use in VirtualEnv
On an Ubuntu 14.04 LTS you should not touch the Python version of the system. But you can compile other Python versions from source and use those runtimes with VirtualEnv. This post shows how.
Updated on Dec 20, 2016: Compile latest version Python-2.7.13, released December 17, 2016. Run the complete test suite.
Introduction
Ubuntu 14.04 LTS will remain using Python 2.7.6. You want a newer Python? What can you do? Several solutions are discussed in “How can I upgrade Python to 2.7.9 on Ubuntu 14.4?”
However Renoir Boulanger is warning you in a blogpost to not change the Python version of Ubuntu system itself:
If you see procedures that show you to use update-alternatives to replace python, don’t do it! Go instead learn how to run your own Python version in VirtualEnv.
Instead Renoir is presenting steps that let you build your own Python runtime from source. I doesn’t interfere with the system and can be used in VirtualEnv situations. But it seems you can skip building a .deb package.
The Modified Recipe
I followed the recipe with little modifications.
Install the build depencencies (I’ve removed some duplicates):
Obsolete:
Renoir is building a .deb package in this step four using fpm, the “Fabulous Package Manager”. It seems this step is not necessary. Instead we can tell ‘virtualenv’ directly about the new Python runtime.
New:
Specify the new runtime when you run ‘virtualenv’:
# go home
➜ cd
# create subfolder for virtual environments
➜ mkdir ~/venvs
# go to base folder for virtual environments
➜ cd ~/venvs
# create and equip a virtualenv folder python2713
➜ virtualenv --python=/usr/local/lib/python2.7.13/bin/python python2713
Running virtualenv with interpreter /usr/local/lib/python2.7.13/bin/python
New python executable in /home/marble/venvs/python2713/bin/python
Installing setuptools, pip, wheel...done.
New:
Switch to the new environment:
# go home
➜ cd
# activate environment
➜ ~ source ~/venvs/python2713/bin/activate
(python2713) ➜ ~
# verify version
(python2713) ➜ ~ python --version
Python 2.7.13
Learn about Running & Writing Tests - if you like. Run the testsuite:
# this does not work with 2.7.13:
(python2713) ➜ ~ python -m test
# this does work with 2.7.13:
(python2713) ➜ ~ python -m test.regrtest -h # show help
(python2713) ➜ ~ python -m test.regrtest # run tests
Or run the tests like this:
(python2713) ➜ ~ python -c "from test import autotest"
Example: virtualenv
We assume PIP and the SetupTools are installed.
Update ‘pip’ and ‘virtualenv’ if available:
# make sure we have the latest 'pip'
➜ sudo pip install --upgrade pip
# make sure we have the latest 'virtualenv'
➜ sudo pip install --upgrade virtualenv
Start a new project:
➜ mkdir ~/venvs
➜ cd ~/venvs
➜ virtualenv --python=/usr/local/lib/python2.7.13/bin/python newproject
Should look like:
Activate the new environment in this terminal instance:
source ~/venvs/newproject/bin/activate
Should look like:
Return to the original environment with deactivate:
Good luck and have fun!
More
Add MySQLdb
When working with Python-2.7.11 I found that pip install python-mysqldb failed even though I allowed access to the ‘system-site-packages’: