DEV Community

Cover image for Learn To Setup Python 3 As A Default On your Mac
Joaquin Guardado
Joaquin Guardado

Posted on

Learn To Setup Python 3 As A Default On your Mac

Minimum requirements

  • Command Line tools or Xcode $ xcode-select --instal
  • Homebrew $ ruby -e "$(curl -fsSL [https://raw.githubusercontent.com/Homebrew/install/master/install](https://raw.githubusercontent.com/Homebrew/install/master/install))"

Python3

At the time of this writing your Macbook ships with an out of date version of Python, version 2.7 which was officially deprecated on Januray 1st, 2020. This means you need to update it to version 3 before building any new projects in your local development environment.

WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jul  5 2020, 02:24:03)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.21) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
Enter fullscreen mode Exit fullscreen mode

My preferred method of achieving this is to use Homebrew a very popular MacOs package manager. This will allow you to switch between Python3 and Python 2 in case you still need it. Please make sure you have it installed before continuing.

Open your terminal and run the following.

$ brew install python
Enter fullscreen mode Exit fullscreen mode

To look at where it is installed run this command to get a similar output.

$ which python3
/usr/local/bin/python3
Enter fullscreen mode Exit fullscreen mode

If you wish to be able to run a command with just python instead of python3 create an alias in your .zshrc. Copy the python3 installation path from above and run the following command.

$ echo "alias python=/usr/local/bin/python3" >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Close your terminal and open it back up. Now if you run the command python you should get the following.

$ python --version
Python 3.8.5
Enter fullscreen mode Exit fullscreen mode

Now if you need Python 2 to work on some older projects run it like this.

$ python2
Enter fullscreen mode Exit fullscreen mode

Aslo If at any time you would like to set Python 2 as default on your Mac again just remove or comment out the alias in your .zshrc with vim or any editor you like.

$ vi ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Edit your .zshrc and comment out the alias.

#alias python=/usr/local/bin/python3
Enter fullscreen mode Exit fullscreen mode

Pip

Finally don't forget to update pip to pip3. The pip command is the default package manager for Python packages and although the default version of python on your system was updated pip is still running on the old version. To fix that create another alias for pip3. This will ensure the packages installed via pip are compatible with the new default version of Python.

First find the location of pip3.

$ which pip3
/usr/local/bin/pip3
Enter fullscreen mode Exit fullscreen mode

Now create the new alias.

$ echo "alias pip=/usr/local/bin/pip3" >> ~/.zshrc 
Enter fullscreen mode Exit fullscreen mode

Close your terminal to reset it and run the pip command.

$ which pip
pip: aliased to /usr/local/bin/pip3
Enter fullscreen mode Exit fullscreen mode

That's it! as always if you have any questions, comments or concerns feel free to reach out to me on Twitter.

Best.

Top comments (5)

Collapse
 
andreimoment profile image
Andrei Andreev

Clear and to the point. Thank you, now I have Python 3 on my mac!

Collapse
 
__e7f2e76810f1fcf0d12e profile image
澤坤 李

This article is wonderful. Thank you for your help~

Collapse
 
blackonthegram profile image
D

I keep getting a syntax error

Collapse
 
samshapland22 profile image
samshapland22

thank you!

Collapse
 
2003aryan profile image
Aryan Garg

this really helped me
thanks