133

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?

Anthony
  • 33,838
  • 42
  • 169
  • 278
  • 1
    When I did this to my fedora the yum or apt-get did not work any more. – User Oct 08 '13 at 19:25
  • @User: Yeah, I broke an ancient Mandrake similarly (changing `/usr/bin/env python` to mean 2.6 instead of 2.3 meant half of the commands in `rpm` and all of `urpmi` stopped working). – abarnert Oct 08 '13 at 19:40
  • I have followed the following step to install Django on Centos 5:1st, install Python 3.6 from source code. 2nd: in shell type the following command "alias python=/usr/local/bin/python3.6" 3rd: run following command to install Django "pip3 install Django" 4th: "python -m django --version" to verify the Django installed with version "1.10.5" – showmyroutes Feb 10 '17 at 13:06
  • for me `/usr/bin/python` was a soft link pointing to `python2` . I just changed it to `python3.6` – munish Aug 11 '17 at 13:32

7 Answers7

181

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    :( you are right. unfortunately, I had already created a new symb link as per @rohit's answer. Now my `yum` command doesn't work! I tried creating the sym link back but it doesn't work `sudo ln -sf /usr/bin/python2.6 /usr/bin/python` any workaround? – Anthony Oct 08 '13 at 22:39
  • @Anthony: My guess is that `/usr/bin/python` wasn't actually a symlink before, it was a wrapper script or executable, and now you've overwritten it and can't get it back. If `rpm` is still working, you can manually download the Python package and install it without `yum`. – abarnert Oct 08 '13 at 22:42
  • I can still get to 2.6 interpreter when i type `python2.6` so it is still there. – Anthony Oct 08 '13 at 22:42
  • 7
    @Anthony: And in the future, if someone tells you to change stuff in /usr/bin (or anywhere in /usr besides /usr/local), make a note of what's there, and a backup… – abarnert Oct 08 '13 at 22:43
  • what a bad mess i got myself in!! I have another centos box which might have that script. If i copy it from there and put it in /usr/bin of this box. do you think it'll work? – Anthony Oct 08 '13 at 22:44
  • @Anthony: Yeah, good chance of that. But make sure they're the exact same Python 2.6 package versions first. Meanwhile, it might just be that `/usr/bin/python` has to be a regular file rather than a symlink, in which case either `cp` or hard-linking might solve it… (I just noticed your `ls -al` command… it was a regular file, not a symlink, that you overwrote, and it had a linkcount of 2, so probably either `cp` or `ln` without `-s` is the answer, but I'm not sure which.) – abarnert Oct 08 '13 at 22:45
  • I've unlinked the 2.7.2 and copies the python script to `/usr/bin`. Now when I type `python' it says "-bash: /usr/local/bin/python: No such file or directory" Is there a way I can make it so that when i type python it runs the script i copied in `/usr/bin` ? – Anthony Oct 08 '13 at 22:54
  • btw, after copying the script yum is working again. So I'm half way there. now just want 2.6 back when i type `python` – Anthony Oct 08 '13 at 22:55
  • phew. risk averted. just had to get out of that bash shell. all is well now – Anthony Oct 08 '13 at 23:00
  • plus 1 for `monkeying ` ... never heard before! – Magno C Jul 19 '17 at 15:20
  • @abarnert: I would like to use `iPython', which when called gives me the error `IPython requires Python version 2.7 or 3.3 or above.`. Now, your answer works for opening Python2.7 just by calling `python`, but how do I make `iPython` understand that I have installed Python2.7 ? – Srivatsan Sep 07 '17 at 00:41
21

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.)

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Possibly it may be necessary to set PYTHONHOME appropriately / unset it if it is set by something. – Marcin Oct 08 '13 at 19:15
  • 1
    This isn't nearly as dangerous as the solutions that suggest changing /usr/bin itself… but I still think it's not what the OP actually wants. – abarnert Oct 08 '13 at 19:45
  • This helped me after upgrading from Ubuntu 14.x to 16.x – learn2day Oct 19 '16 at 18:35
8

Enter the command

which python

//output:
/usr/bin/python

cd /usr/bin
ls -l

Here you can see something like this

lrwxrwxrwx 1 root   root            9 Mar  7 17:04  python -> python2.7

your default python2.7 is soft linked to the text 'python'

So remove the softlink python

sudo rm -r python

then retry the above command

ls -l

you can see the softlink is removed

-rwxr-xr-x 1 root   root      3670448 Nov 12 20:01  python2.7

Then create a new softlink for python3.6

ln -s /usr/bin/python3.6 python

Then try the command python in terminal

//output:
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux

Type help, copyright, credits or license for more information.

vahdet
  • 6,357
  • 9
  • 51
  • 106
Sreenath
  • 91
  • 1
  • 5
  • 1
    Your post is not an answer to OP's question. – Frank Mar 07 '19 at 12:10
  • This solved a very odd problem for me. When i check the python version from root or normal terminal, it showed 3.6.8 but if I create a .sh and type python -version inside, that printed 2.7. Turns out it was because a softlink was existing as mentioned above. Followed the steps and it's fixed. Thanks. – S4nd33p Aug 13 '19 at 08:16
4

Verify current version of python by:

$ python --version

then check python is symbolic link to which file.

  $ ll /usr/bin/python

Output Ex:

 lrwxrwxrwx 1 root root 9 Jun 16  2014 /usr/bin/python -> python2.7*

Check other available versions of python:

$ ls /usr/bin/python*

Output Ex:

/usr/bin/python     /usr/bin/python2.7-config  /usr/bin/python3.4         /usr/bin/python3.4m-config  /usr/bin/python3.6m         /usr/bin/python3m
/usr/bin/python2    /usr/bin/python2-config    /usr/bin/python3.4-config  /usr/bin/python3.6          /usr/bin/python3.6m-config  /usr/bin/python3m-config
/usr/bin/python2.7  /usr/bin/python3           /usr/bin/python3.4m        /usr/bin/python3.6-config   /usr/bin/python3-config     /usr/bin/python-config

If want to change current version of python to 3.6 version edit file ~/.bashrc:

vim ~/.bashrc

add below line in the end of file and save:

alias python=/usr/local/bin/python3.6

To install pip for python 3.6

$ sudo apt-get install python3.6 python3.6-dev
$ sudo curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python3.6
$ sudo easy_install pip

On Success, check current version of pip:

$ pip3 -V

Output Ex:

pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.6)
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
2

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"
1

Simplest approach; these three commands will help you set,

Python 2.x to 3.x

  1. see python version, use python --version (let you got installed one is 2.7.x)
  2. find where the Python 3 is installed, use which python3 ( or which python gives you current installation of python version)
  3. Last step, use aliasing, alias python=/usr/bin/python3.6 (one in above step)
  4. Now, run again, python --version, you will find, 3.6.x installed.

Python 3.x to 2.x (Almost Same)

  1. see python version, use python --version (let you got installed one is 3.6.x)
  2. find where the Python 2 is installed, use which python2 (which python gives you where current version of python is installed.)
  3. Last step, use aliasing, alias python=/usr/bin/python2.7 (one you get in above step)
  4. Now, run again, python --version, you will find, 2.x.x installed.
Hari Kishore
  • 2,201
  • 2
  • 19
  • 28
-8

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.

emesik
  • 221
  • 1
  • 9
  • 5
    The Python package is almost certainly required by the distro. CentOS is RPM-based, and half the RPM and YUM tools are written in Python. – abarnert Oct 08 '13 at 19:41
  • Dear Michael, Please advise option to uninstall manually installed python2.7! – AVA Feb 05 '15 at 14:31
  • The simple solution would be removing everything under /usr/local/ but this way you remove everything that you have compiled and installed manually. There is no good way to trace which files belong to what software in /usr/local and for this reason installing anything there is generally discouraged. Perhaps you have only python there. If /usr/local/bin contains only python executables, you have a good chance that removal won't break anything else. – emesik Feb 10 '15 at 22:15
  • 2
    You should never uninstall python. Take this from me. I did it and it broke my system. Lots of apps depend on it. This is very ill-advised. – Patrick Mutwiri Apr 12 '18 at 06:10