There are many ways to configure your mac to use the desired version of Python. Python 2.7 has reached its end of life on January 1st, 2020.
I had attempted many ways to configure my Mac to use python3. However, not all options are the correct approach. I believe the right way is to use ‘pyenv’.
“System Python” is the Python that comes installed on your operating system. If you’re on Mac or Linux, then by default, when you type
python in your terminal, you get a nice Python REPL.
So, why not use it? One way to look at it is that this Python really belongs to the operating system. After all, it came installed with the operating system. That’s even reflected when you run
$ which python
Option 1: Replace Old Version (Not Recommended):
- Replace the old version with the new
$> ln -s -f /usr/local/bin/python3.8 /usr/bin/python
Error: Operation not permitted
MacOS protect the system version of python in /usr/bin/python. This is exactly what we shouldn’t do to change the system version of python.
Don’t try to change the system version of Python. It won’t work as it is protected by the OS.
2. Use Alias
$> echo “alias python=/usr/local/bin/python3.7” >> ~/.zshrc
3. Use Homebrew to install python
# Installing latest Python by using HomeBrew (https://brew.sh/)
$> brew install python# Check the location and version
$> ls -l /usr/local/bin/python*# Change the default python symlink to the specific version
$> ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
You should get the Python version that you just link it
$> python --version
Option 2: Using pyenv (Recommended — The Correct Way)
‘pyenv’ is a recommended way to manage multiple Python versions.
# Install pyenv using Homebrew
$ brew install pyenv# To list all available CPython versions from 3.6 to 3.8
$ pyenv install --list | grep " 3\."# To install a python version using pyenv
$ pyenv install -v 3.8.5# To uninstall a python version
$ pyenv uninstall 2.7.15
# To set a Python Version to use
$ pyenv global 3.8.5
# Say, if you want to switch back to system version
$ pyenv global system# To list all the available version installed on your computer
$> pyenv versions
* system (set by /home/realpython/.pyenv/version)
note: * indicate the version currently in use
Remember to add this to ~/.zshrc (may be ~/.bash_profile for you)
The power of pyenv comes from its control over the shell’s path. In order for it to work correctly, we need to add the following to our configuration file (.zshrc ):
$> echo -e ‘if command -v pyenv 1>/dev/null 2>&1; then\n eval “$(pyenv init -)”\nfi’ >> ~/.zshrc
For further reading, refer to https://realpython.com/intro-to-pyenv/