Set Python 3 as default on macOS

Firstly, macOS Big Sur is the best operating system ever built, period. It elevates your Mac to a new level of power and beauty with a refined new design, major app updates, and more transparency around your privacy.

I’ve been using Big Sur for a month now (delayed installing it because of some Logic Pro plugings and they still aren’t compatible 😜) and I haven’t found one single problem using it.

But today, I started building an app and needed to refresh some of my Python memories. After opening the terminal I wrote πŸ‘‡

python --version

And the output was

Python 2.7.16

Sad, isn’t it?

We have decided that January 1, 2020, was the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it. You should upgrade to Python 3 as soon as you can.

Now, that’s a quote from python.org.

So, I had to install the latest version of Python which is 3.9.1, as of today. I heard about pyenv from Stack Overflow and didn’t look for an alternative. I just went ahead start working with it. I installed it through Homebrew, which I always install right after installing Command Line Tools for Xcode…

brew install pyenv🍺  /usr/local/Cellar/pyenv/1.2.22: 721 files, 2.6MB

Then, I installed the latest version of Python …

pyenv install 3.9.1python-build: use openssl@1.1 from homebrewpython-build: use readline from homebrewDownloading Python-3.9.1.tar.xz...-> https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xzInstalling Python-3.9.1...python-build: use readline from homebrewpython-build: use zlib from xcode sdkInstalled Python-3.9.1 to /Users/shuvo/.pyenv/versions/3.9.1

Then, I had to make it the global default version…

pyenv global 3.9.1# Then double checking the versionshuvo@Syeds-MacBook-Pro ~ % pyenv version3.9.1 (set by /Users/shuvo/.pyenv/version)

To make it work properly I needed to add the following to .zshrc

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc

Then I went to check my .zshrc file and saw the lines below were added to it…

if command -v pyenv 1>/dev/null 2>&1; then  eval "$(pyenv init -)"fi

Then I went to check the Python version again…

python --version   Python 2.7.16

WTF? 🀬 I got angry and thought to mess around with the path…

PATH=$(pyenv root)/shims:$PATH

Checked again…

python --versionPython 3.9.1

Awesome, isn’t it? πŸ˜ƒ

Now, if you take look at this post and get scared seeing all those codes, don’t be, most of them are outputs πŸ˜‚ and this is the easiest way to install Python 3 on your Mac, trust me…

Leave a Comment