If your system came with Python 2.7, you can keep it with Python 2.7.10. and your system came with Python 2.6, you can not change the default Python system to 2.7: you're going to destroy your system - most Linux distributions have several key programs for administration that depend heavily on Python on the system.
The correct way to do this is to use the system manager to download the prerequisites for compiling Python (for example, in Debian, Ubuntu and derivatives: "apt-get build-dep python-2.7", in fedora : yum-builddep python2). -Download the source code (the .tar.gz file) from Python.org, and compile a local version for the user - set the prefix to be inside your own Home (./configure --prefix = / home / user / usr) or a separate system prefix of /usr
- /opt
is a good request. The advantage of using a prefix within your / home is that you do not need super-user ( sudo
) permissions at any time to install the new version of Python.
So, install the virtualenv package in the system Python, and for each project it works on, create a virtualenv, using the Python you have compiled - to do this use the
-p
of virtualenv argument.
Each time you work on the project, activate virtualenv. The sequence, after compiling the new Python, is this:
apt-get install python-virtualenv
virtualenv -p /home/usuario/usr/bin/python-2.7 meuprojeto
cd meuprojeto
source bin/activate
and from there place the .py of your new project inside the folder - and install other modules at will with the command pip
:
the installed modules will be restricted to virtualenv. A second project that uses different versions of the same modules can be created in another folder, and one project will not interfere with the other.
(The source bin/activate
step must be executed on every shell you are running Python programs of this project - there are virtualenv helper packages that allow a simpler command to activate virtualenvs, but the difference is almost cosmetic only)
This recipe works the same way for newer versions of Python - and you can even have separate virtualenvs with Python 3.4 and Python 3.5 beta.