How to make a program use python2.7 instead of default python3.4 on ubuntu 14.04

27,423

Solution 1

Check your ~/.bashrc and/or .bash_aliases to find if you have any kind of alias like this:

alias python=python3

Change it accordingly to python2.7

Solution 2

Normally, in Ubuntu, python defaults to python2. So you probably have a

#!/usr/bin/env python3 
# 

or similar in the top of your script. Either if this is the case or if you have changed the defaults, in the header of your program, change

#!/usr/bin/env python
# 

(or the more probable python3) to

#!/usr/bin/env python2
# 

In standard Ubuntu, python should automatically point to python2:

[romano:~/tmp] % ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 jul 22 09:49 /usr/bin/python -> python2.7

so normally the above thing should not be needed. But python2 and python3 points respectively to the standard version of version 2 and 3, so if you explicitly want one version, simply tell it.

Share:
27,423

Related videos on Youtube

Aymen
Author by

Aymen

Updated on September 18, 2022

Comments

  • Aymen
    Aymen over 1 year

    I am using ubuntu 14.04 and I have a program that depends on python 2.7 and does not support any of python 3.x. Ubuntu 14.04 comes with python3.4 by default. I would like to know if there is a possibility to make my program uses python2.7 instead of the default version. I thought about uninstalling python3.4, but I think it is not a good idea as some other programs may depends on it.

    Thanks

    • Timo
      Timo over 9 years
      Python 2.7 is the default in 14.04.
    • Aymen
      Aymen over 9 years
      When I type $python it appears python 3.4
    • Andor
      Andor over 9 years
      Do you have your python 2.7 installed? So if you run 'python2.7' it gives you the python shell...
    • Aymen
      Aymen over 9 years
      After I installed python2.7 I can get python2.7 shell when I type $python. Before only python3.4 was installed. So can I assume now that a program which depends on python2.7 will run without problems with both versions coexisting?
    • Andor
      Andor over 9 years
      Both python versions can coexist at the same time, you just have to call the version you want for your software.
    • Jacob Vlijm
      Jacob Vlijm over 9 years
      just read the linked post in reversed direction :)
    • Aymen
      Aymen over 9 years
      Jacob you have right. Depending on shebang defining version, present in python script, the appropriate python version will be used providing it is installed of course.
    • Aymen
      Aymen over 9 years
      Thank you everybody. I am completely ignorant about python, I just want it as a dependency for my target program. Thanks!
  • Aymen
    Aymen over 9 years
    I don't have .bash_aliases file and .bashrc file does not contain an alias for python. Shall I add it?
  • Andor
    Andor over 9 years
    You can do it for convenience. Also, do an: 'ls -l /usr/bin/python' to check where it's linked.
  • Aymen
    Aymen over 9 years
    /usr/bin/python links to python2.7. But, what I would like to do is that one program use python while others continue to use python3.4. I assume that when a program depending on python3.4 is executed it will call soft link python3 not python. Is my assumption correct?
  • Andor
    Andor over 9 years
    Depends on how it's programmed. Check stackoverflow.com/questions/11170827/…