EDIT: *As @Anthon has pointed out in the comments, this PPA does not include Python 2.7 for Ubuntu 12.04 (Precise). See funky-future's answer below, which I don't believe existed when I originally responded.
The preferred "Ubuntu" way to deal with packages not included in their universe by default is to use a PPA (third party repository), rather than manually compiling. You maintain the benefits of package management, updates, and dependency resolution this way.
However, the third party repositories are NOT officially supported, if that is part of your requirement.
The DeadSnakes (get it) PPA maintains Python releases not included in the systems package management.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python2.7
You can read more about this PPA here.
However, in your case this does not include Python 2.7, as DeadSnakes only provides major versions not included and not point releases. As Ubuntu already packages 2.7 for Ubuntu 12.04, DeadSnakes will not provide this.
It might be preferable for you to make the case that you should not use the native packaging with Ubuntu in this case. See this excerpt from DeadSnakes;
Using third-party modules packaged for Debian or Ubuntu with the Python
interpreters from this repository is a bit of a mixed bag. For Python 2, Python
modules from the official repositories will not work, as a consequence of how
Python packaging works in Debian. For Python 3 on the other hand, all
pure-Python module packages at least should be available; compiled extension
modules will not work however.
In general, you're better off installing Python modules using the common Python
packaging tools rather than the system package manager. For an introduction into
the Python packaging ecosystem and its tools, refer to;
A few of these tools might also be provided in this repository as a convenience.
If you are absolutely required to use the operating systems package management, and are unable to move to a different version of Ubuntu with a package provided by DeadSnakes, or natively, than you only have two options left, as I see it.
- You could request a backport of Python 2.7.5 to Ubuntu 12.04.
- You could create your own PPA with the version of Python you require.
side note: AskUbuntu is a better StackExchange to post stuff like this to, than SuperUser generally.