How to change python3 path pointer?

I am using Ubuntu 20.04.2 LTS with python version 3.9.5 installed.

When I type which python in the terminal, it shows /usr/bin/python which is the default path for python2 (2.7 in my case). But when I type which python3, it shows /usr/local/bin/python3, not the usual default path (/usr/bin/python3) for python3.

I've checked my /usr/bin folder and I did notice that there are files named python3 and python3.9. I just want my python3 path variable to point to the location /usr/bin/python3 or /usr/bin/python3.9. What steps should I take to do that?

P.S., I don't want to set default python to python3, of which instructions I found in the ask-ubuntu forum and also in many articles after some googling. I just want to set my python3 path to /usr/bin/python3 or /usr/bin/python3.9


Solution 1:

As long as a custom python3 is installed in /usr/local/bin/, it will show up as the default python3 executable. why? Because by default, /usr/local/bin/ is listed earlier in the search path then /usr/bin. This way, your custom executables have priority over these installed by the system.

To make the system find /usr/bin/python3 first, you must move out /usr/local/bin/python3. The alternative would be to ticker with your PATH, moving /usr/local/bin behind, but that would affect every other custom executable.

Thus, rename /usr/local/bin/python3 to /usr/local/bin/python3cust or something. Then, you will be using the default python version.

On Ubuntu, you actually may not change the default python version. Python is extensively used in the operating system. Changing the version will likely break things.