Ubuntu – Make Python3.6 the default interpreter when running the command python filename.py in terminal

pythonpython3

I have three versions of python on ubuntu 18.04
when running python -V in terminal it produces Python 2.7.15rc1
and when running python3 -V it produces Python 3.7.2

You could see the paths of python versions on my OS from this picture here is the screeshot

but when running pip install package-name or pip3 install package-name it installs the package in python3.6,

Now as all the packages installed on python3.6, I want to run my programs in terminal on python3.6 not Python 2.7.15rc1 or Python 3.7.2
as in the picture

My problem is:
when I run python filename.py, it interpreted on Python 2.7.15rc1 so it produces an error that says package not found, and the same when running python3 filename.py it interpreted on Python 3.7.2 and it produces an error that says package not found

Now I want to set Python3.6 to be the default interpreter when I run a program in the terminal because it has all the packages installed to it!

Best Answer

On your system /usr/bin/python3 is likely a link to /usr/bin/python3.7 and can be changed to be /usr/bin/python3.6. You will also find that /usr/bin/python is a link to /usr/bin/python2.7×

The link for python3 can be changed to be /usr/bin/python3.6 but making python itself default to python3 is fraught with peril, because your system may have scripts written for python v2(*) with a #! /usr/bin/python shebang and changing python to be a python V3 interpreter will break them.

Another solution is to define a shell alias (in .bashrc)

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

The good thing about this solution is that it only changes the meaning of python for interactive shells, in scripts the aliases are ignored and you still use the default python interpreter and so won't break anything.

(*) Find them with:

find /usr/ -type f -executable -exec grep -E -l '#!.+python[^3]' {} \;
Related Question