Python comes pre-installed on Mac OS, but usually it’s an older version. So even if you install the latest version of Python i.e. Python 3 – 3.10, 3.9, 3.8 or 3.7; sometimes, the Mac terminal still keeps on using an older version when type in the command ‘Python’. So if you type in the ‘python –version’ command in your terminal, it will usually show an old version of Python; for me, it was Python 2.7.16.
One way to get around this is using the Python3 command to call for the latest version of Python. But that’s not for handy as folks are used to the ‘Python’ command. So if you want the Terminal to use the latest version of Python i.e. Python 3 when typing the ‘Python’ command, here’s how to do it:
Open your Mac OS terminal and type in this command:
It will show you where your latest version of Python is installed. Usually, it will output something like this:
If you don’t see your Python directory here, then you will probably have to reinstall the latest version of Python. Alternatively, you will have to manually set the PATH variable for Python. I’ll post a detailed guide on this soon.
Now, if you go to Mac Finder and press ‘Command + Shift + G‘, you will be able to type in this directory location i.e. /Library/Frameworks/Python.framework/Versions/3.10/bin to access your Python installation directory.
Finder is not really suitable for what we are going to do next. So hop back into your Mac terminal and go to the Python directory by typing in this command.
Now if you type the follow command in this directory:
It will output something like this:
lrwxr-xr-x 1 root admin 9 Apr 28 03:39 2to3 -> 2to3-3.10 -rwxrwxr-x 1 root admin 142 Mar 24 03:02 2to3-3.10 lrwxr-xr-x 1 root admin 8 Apr 28 03:39 idle3 -> idle3.10 -rwxrwxr-x 1 root admin 140 Mar 24 03:02 idle3.10 -rwxrwxr-x 1 root admin 270 Apr 26 03:59 pip3 -rwxrwxr-x 1 root admin 270 Apr 26 03:59 pip3.10 lrwxr-xr-x 1 root admin 9 Apr 28 03:39 pydoc3 -> pydoc3.10 -rwxrwxr-x 1 root admin 125 Mar 24 03:02 pydoc3.10 lrwxr-xr-x 1 root admin 10 Apr 28 03:39 python3 -> python3.10 lrwxr-xr-x 1 root admin 17 Apr 28 03:39 python3-config -> python3.10-config lrwxr-xr-x 1 root admin 18 Apr 28 03:39 python3-intel64 -> python3.10-intel64 -rwxrwxr-x@ 1 root admin 152624 Mar 24 03:06 python3.10 -rwxrwxr-x 1 root admin 2084 Mar 24 03:02 python3.10-config -rwxrwxr-x 1 root admin 70544 Mar 24 03:06 python3.10-intel64
You can see above that the ‘python3’ file (this is called a symbolic link or a soft link file) that connects to an executable file python 3.10. So when you type in the ‘python 3’ command, it tells the terminal to open the python3.10 executable program.
Now all we need to do is create a new symbolic link file called ‘python’ in that directory and link it to ‘python3.10’ using this command:
ln -s python3.10 python
Now you should see a shortcut-type file in this directory like this:
Restart your Terminal. You should now see the latest version of Python when you type in the ‘Python’ command.
So this was how to force Mac OS terminal to use the latest version of Python. I hope this guide helped you. Keep learing!