44

I type python in a terminal window. I get Python 2.7 and not Python 3.5. According to the docs, Python 3.5 should come preloaded.

3
  • 1
    If you set up a virtual environment using: "python3 -m venv my-venv", then from any terminal activate that virtual environment by calling "source .../my-env/bin/activate", then python 3.5 will be the default in that terminal. You can check the current default version by calling "python -V". Aug 13, 2017 at 16:28
  • while your answer is helpful, i asked why. not how to. and, that is the big debate. here. why should an older version by the default, instead of the current version.
    – givonz
    Aug 14, 2017 at 19:10
  • 1
    You are exactly correct about my "comment"! Which is why I wrote a comment and not an answer. Aug 14, 2017 at 21:10

5 Answers 5

58

I don't know what "docs" you are reading nor what they say, but the /usr/bin/python is just a symbolic link to the default version of python, in this case 2.7, which is the result of just typing python on the terminal. This is the result of PEP 394 which defines that

  • for the time being, all distributions should ensure that python refers to the same target as python2.

type python and file /usr/bin/python will confirm this.

The reason for this arrangement other than the convention, is that the source package python-defaults in Xenial is the version 2.7.11, so the python symbolic link points to this version.

To use python 3 you have to be explicit and type python3 in the command line, which is recommended in case you need an specific version, this can also be done with python 2, typing python2. Ubuntu includes both python 2 and 3 versions by default on all current versions.

There are plans to migrate everything to python 3 and marking it as the default.

2
  • This is probably breaking rhythmbox-zeitgeist plugin which depends on zeitgeiigeist-python3 but could not find it. May 31, 2016 at 5:10
  • 2
    This is the doc that the OP means.
    – SaidbakR
    Sep 1, 2016 at 19:52
10

To get the Python 3.5 REPL or run a script that's compatible, type python3. This is to maintain compatibility with all the legacy python 2.x (which has always been, and will remain linked to python).

The dash could find python if you set up a .desktop file for it (in ~/.local/share/applications/), to open a terminal and start the REPL for instance. There isn't a default GUI REPL environment for python, and normal interface expectations are that the user would go to the terminal.

The talk of python 3.5 being default is the maintainers porting all system scripts from python 2.x over to python 3.x, not that python 3 would replace python 2 in all cases.

7
  • so, i guess the correct question is, how do i add python3 into my path, so that it executes automatically?
    – givonz
    May 25, 2016 at 1:27
  • do you mean, how do you get python scripts to execute like a compiled program? May 25, 2016 at 1:31
  • got it thanks. just type python3 at the command prompt.
    – givonz
    May 25, 2016 at 1:36
  • 2
    AIUI what to do with /usr/bin/python in the long term is still an open question. Best practice for python2 scripts would be to use python2 explicitly. May 25, 2016 at 11:37
  • 1
    rather than using #!/usr/bin/python3 (or #!/usr/bin/python2) in scripts, better to use (just for python, specifically) #!/usr/bin/env python3 if you want it to be remotely portable. For the most part, this is just documentation, since in many cases it will be called using $ python3 your_pyprogram.py anyway.
    – michael
    Oct 5, 2016 at 2:28
7

You assume that the default Python should be available as python. That assumption is wrong.

It is an unlucky situation that obsolete Python 2 (python) comes first during shell Tab-expansion, before current and default Python 3 (python3) on systems which have both. The naming itself certainly does not help finding the default either, but there is a reason behind that.

On a system that comes with Python 3 “preloaded” and no Python 2 at all, there will be only python3 and no python.

Code written for Python 3 should always look for an interpreter called python3. Code written for Python 2 will historically look for python.

This way, there will be no extra and unnecessary breakage, neither for new nor old scripts from any uncertainty of what python might point to.

There might come a point in time, possibly decades from now, where almost no-one remembers Python 2, when python might start to refer to python3. This would be purely for convenience, and can and should not be done premature, if at all! There is no hurry, because of the following impact:

“Current old” systems will have no knowledge of python2, only python, so it makes perfect sense to keep Python 2 code looking for python. This is the reason why python should refer to Python 2 for “as long as Python 2 code exists”.

New code written for Python 2, if that makes sense, can perfectly look for python. It might also look for python2, if it is intended to run only on Python 3 aware and well groomed systems (which might offer such link).

The benefit of Python 2 code using python2 would be that it needs no extra touching once python starts referring to python3, which might never happen, or once python disappears at all, which might or might not happen. (That might come true, if Python 2 code dies out after hypothetical Python 4 becomes popular with its interpreter python4. – Because letting it point to either python3 or python4 would both be confusing.)

Note: The Arch Linux distribution did not follow the recommendation and lets python point to Python 3.

2

Python both 2.x, 3.x versions are available. if you call python as it is pointed to python 2.x it will load python 2.x

Below image will help you.

enter image description here

6
  • 1
    how do you type the same command and get 2 different results?
    – givonz
    May 25, 2016 at 1:26
  • 4
    the different output from the second invocation of python is from using <tab> to query the autocompleter, which then outputs the possible permutations of python* and returns you to a new prompt. May 25, 2016 at 1:27
  • @givonz , hmm I used TAB key bro. its not output. it is possible binary pattern.
    – Raja G
    May 25, 2016 at 1:27
  • Negative vote for what?
    – Raja G
    May 25, 2016 at 2:16
  • 2
    I didn't downvote but I think that the answer should explain why the 2 first outputs are different and what is the role of apt-cache, especially for Ubuntu beginners.
    – A.L
    May 25, 2016 at 9:06
2

Python 3.x should work with 16.04 - Actually, it definitely WILL work. I have Ubuntu 14.04 and I have Python 3.4.3 - All you have to do to test it is type in python3 and it should tell you what version you're running.

3
  • 2
    Well I wouldn't call using an OS computer science. And strictly speaking: Ubuntu is a Linux based OS and not Unix based.
    – magu_
    May 25, 2016 at 15:47
  • i have yet to understand the diff bet linux & unix besides some command prompts & differently, but closely worded commands. although the gui is far more advanced than what i see in solaris.
    – givonz
    May 26, 2016 at 10:23
  • 1
    -1 This doesn't address the question of why. May 26, 2016 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .