I\'ve got two versions of python on my linuxbox:
$python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>>
$ /usr/local/bin/python2.7
Python 2.7.3 (default, Oct 8 2013, 15:53:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>>
$ which python
/usr/bin/python
$ ls -al /usr/bin/python
-rwxr-xr-x. 2 root root 4864 Jul 10 22:49 /usr/bin/python
How can I make 2.7 be the default version so when I type python
it puts me in 2.7?
You probably don\'t actually want to change your default Python.
Your distro installed a standard system Python in /usr/bin
, and may have scripts that depend on this being present, and selected by #! /usr/bin/env python
. You can usually get away with running Python 2.6 scripts in 2.7, but do you want to risk it?
On top of that, monkeying with /usr/bin
can break your package manager\'s ability to manage packages. And changing the order of directories in your PATH
will affect a lot of other things besides Python. (In fact, it\'s more common to have /usr/local/bin
ahead of /usr/bin
, and it may be what you actually want—but if you have it the other way around, presumably there\'s a good reason for that.)
But you don\'t need to change your default Python to get the system to run 2.7 when you type python
.
First, you can set up a shell alias:
alias python=/usr/local/bin/python2.7
Type that at a prompt, or put it in your ~/.bashrc
if you want the change to be persistent, and now when you type python
it runs your chosen 2.7, but when some program on your system tries to run a script with /usr/bin/env python
it runs the standard 2.6.
Alternatively, just create a virtual environment out of your 2.7 (or separate venvs for different projects), and do your work inside the venv.
Add /usr/local/bin
to your PATH
environment variable, earlier in the list than /usr/bin
.
Generally this is done in your shell\'s rc file, e.g. for bash, you\'d put this in .bashrc
:
export PATH=\"/usr/local/bin:$PATH\"
This will cause your shell to look first for a python
in /usr/local/bin
, before it goes with the one in /usr/bin
.
(Of course, this means you also need to have /usr/local/bin/python
point to python2.7
- if it doesn\'t already, you\'ll need to symlink it.)
All OS comes with a default version of python and it resides in /usr/bin. All scripts that come with the OS (e.g. yum) point this version of python residing in /usr/bin.
When you want to install a new version of python you do not want to break the existing scripts which may not work with new version of python.
The right way of doing this is to install the python as an alternate version.
e.g.
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar xf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure --prefix=/usr/local/
make && make altinstall
Now by doing this the existing scripts like yum still work with /usr/bin/python.
and your default python version would be the one installed in /usr/local/bin.
i.e. when you type python you would get 2.7.3
This happens because. $PATH variable has /usr/local/bin before usr/bin.
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
If python2.7 still does not take effect as the default python version you would need to do
export PATH=\"/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin\"
I guess you have installed the 2.7 version manually, while 2.6 comes from a package?
The simple answer is: uninstall python package.
The more complex one is: do not install manually in /usr/local. Build a package with 2.7 version and then upgrade.
Package handling depends on what distribution you use.