NOTE: I’m learning Python, this was a bump in the road.

Let’s get to the point, I’m working in Ubuntu, why does running python in the terminal not execute the latest installed version of Python 3?

The need to support legacy dependency libraries using older versions of Python 2

When executing the python command in terminal it will execute Python 2.7. You need to explicitly run python3 to execute Python 3.x. This is the same for running pip (pip vs pip3). You need to be explicit when targeting a specific Python build.

While this is troublesome, it is not advised to remove the symbolic link or replace this older version of Python. For obvious reasons, you may find that dependencies and builds might stop working.

There is a plan to default to the latest version, but there does not seem to be any concrete timelines. Again, the reason being for on-going support of legacy libraries which need to be migrated, and are probably no longer maintained.

This is from an out of date Ubuntu page, on project goals:

  • /usr/bin/python will point to Python 3. No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future). /usr/bin/python and /usr/bin/python2 will point to Python 2.7 and /usr/bin/python3 will point to the latest supported Python 3 version.
  • Python 2 will be removed from the archive. No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020. It would be nice if we could demote Python 2 to universe, but that’s currently problematic for technical reasons relating to multi-Python version support in Debian/Ubuntu.

Use an Alias

It seems that the safest and easiest work-around is to use an alias in your ~/.bashrc or .bash_aliases file

alias python=python3

Or

alias python='/usr/bin/python3'

After adding the above in the file, run source ~/.bashrc or source ~/.bash_aliases.

Reference

https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3

Leave a Reply