January 12, 2022

Tutorial: Installing Python on Mac

Python is one of the most powerful programming languages, mostly used in data science, machine learning, and big data analytics. So, installing Python is essential for all programmers. As a novice programmer, you may be wondering how to install or update Python on your Mac properly. Here, we will walk through the different ways of installing and updating Python on macOS.

Then, to write and run our Python code in an integrated development environment (IDE), we will learn how to install and configure Visual Studio Code on Mac. There are different methods for installing and updating Python on Mac, but let's stick to the third principle of the Zen of Python that says: “Simple is better than complex.” Accordingly, we will try simple methods rather than complex ones. Before we jump into learning how to install or update Python on Mac, let's review what we're going to discuss in this tutorial:

Installing and Updating Python on Mac

I have two pieces of news for you; one is good, the other bad. The good news is that for the sake of compatibility with legacy systems, Python 2.7 is pre-installed on your Mac, but the bad news is that Python 2.7 has been retired. Therefore, it isn't recommended for new developments. So, if you want to take advantage of the new Python version with its many features and improvements, you need to install the latest Python alongside the version that comes pre-installed on macOS. Before we start installing the latest version of Python, let’s see why there are different versions of the same programming language. All programming languages evolve by adding new features over time. The programming language developers announce these changes and improvements by increasing the version number.

Install Python 3 as a part of the Command Line Developer Tools

To check the current version of Python that is already installed, open the Terminal application by typing command + space and then spelling out terminal and hitting return. Now, type the following command, and then hit return to see that you have Python 2.7 pre-installed on your Mac:

Python 2.7.18

Now, try the following command to check whether or not Python 3 is installed on your Mac:

~

The following message will probably appear on the Terminal window,

xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.

And alongside the Terminal window, a dialog box will automatically appear that says this command requires the command line developer tools. First, let’s identify the command-line developer tools. To put it briefly, the command line developer tools package is a set of tools mostly used in the development process. They help run specific commands, such as make, git, python3, etc. So, although there are other ways to install Python 3.x on Mac without installing the command line developer tools, I recommend you install it because it provides a string of tools for development on Mac. To install the package, click on the Install button, and follow the steps to complete the installation process. Once the installation process is complete, rerun the previous command. Yes, Python 3.x is installed on your Mac.

~ Python 3.8.9

Install Python 3 with the Official Installer

Downloading the latest Python version from the official Python website (python.org) is the most common (and recommended) method for installing Python on a Mac. Let’s try it out.

1. First, download an installer package from the Python website. To do that, visit https://www.python.org/downloads/ on your Mac; it detects your operating system automatically and shows a big button for downloading the latest version of Python installer on your Mac. If it doesn't, click the macOS link and choose the latest Python release.
Installing Python on Mac

2. Once the download is complete, double-click the package to start installing Python. The installer will walk you through a wizard to complete the installation, and in most cases, the default settings work well, so install it like the other applications on macOS. You may also have to enter your Mac password to let it know that you agree with installing Python.
Installing Python on Mac


NOTE If you're using Apple M1 Mac, you need to install Rosetta. Rosetta enables Intel-based features to run on Apple silicon Macs.


3. When the installation completes, it will open up the Python folder.
Installing Python on Mac

4. Let’s verify that the latest version of Python and IDLE installed correctly. To do that, double-click IDLE, which is the integrated development environment shipped with Python. If everything works correctly, IDLE shows the Python shell as follows:
Installing Python on Mac

5. Let’s write a simple Python code and run it in IDLE. Type the following statement, and hit the return key.

print(“Hello, World!”)

Installing Python on Mac

Or let’s do a basic calculation in Python as follows:
Installing Python on Mac

 


NOTE The exciting advantage of this installation method is that you can easily update an outdated Python install by downloading the latest Python installer. The new version of Python is available on your Mac once the installation is complete.


Installing Visual Studio Code on Mac

Although we can use IDLE or the terminal application for writing Python scripts, we prefer to use a powerful, extensible, and lightweight code editor rather than rigid coding environments. In this part of the tutorial, we're going to install Visual Studio Code for Python development on macOS.

1. First, you need to download Visual Studio Code for macOS from its official website at https://code.visualstudio.com/.
Installing Visual Studio Code on Mac
2. Double-click the downloaded file to extract the archived contents.
Installing Visual Studio Code on Mac
3. Move the Visual Studio Code application to the Application folder to make it available in the macOS launchpad.
Installing Visual Studio Code on Mac
4. Launch Visual Studio Code, and then open a folder where your Python scripts exist (or create a new one). For example, create a new folder on your Desktop, and name it py_scripts, then try to open the folder on VS Code. Usually, VS Code needs your permission to access files in your Desktop folder; click OK.
Installing Visual Studio Code on Mac

 

Also, you may need to declare that you trust the authors of the files stored in your Desktop folder.
Installing Visual Studio Code on Mac
5. Create a new file with a .py extension. For example, create a new file, and name it prog_01.py. VS Code detects the .py extension and wants to install a Python extension.
Installing Python on Mac
To work with Python inside VS Code, we need to use the Python extension, which includes many useful features, such as code completion with IntelliSense, debugging, unit testing support, etc.

Install it by clicking on the Install button.
Installing Python on Mac

 

We can also install the Python extension by browsing extensions. Click on the Extensions icon on the left side of VS Code.
Installing Python on Mac

This will show you a list of the most popular VS Code extensions on the VS Code Marketplace. Now, we can select the Python extension and install it.
Installing Python on Mac

 

6. Once the extension is installed, you have to select a Python interpreter. Click on the Select Python Interpreter button:
Installing Python on Mac

Then select the recommended Python interpreter on the list.
Installing Python on Mac

 

If you have multiple Python versions installed on your Mac, it's better to choose the latest version.
Installing Python on Mac

 

You also can select a Python interpreter using the Python: Select Interpreter command from the Command Palette. To do so, press CMD + SHIFT + P, type Python, and choose Select Interpreter.

Running Our First Python Script on Mac

Excellent, we have everything we need to write and run our Python code inside VS Code. Let's write the following code in VS Code and then run it.

print("Hello, World!)
name = input("What's your name? ")
print("Hello {}!\nWelcome to Dataquest!".format(name))

Run the code by clicking the ▶️ button at the top right corner of VS Code. First, it shows Hello, World! in the integrated terminal, then it asks your name; enter your name, and hit return. It outputs Hello <your name,>, and writes Welcome to Dataquest!on the next line.

Installing Python on Mac

Conclusion

In this tutorial, we learned how to install the latest version of Python on Mac, as well as updating an outdated Python version. Additionally, we learned how to install Visual Studio Code as a code editor and configure it for running Python scripts. Finally, we wrote a small Python script in VS Code and ran it successfully. In the next tutorial, we will learn about Python virtual environments and how to create and use them. Congratulations! From now on, the sky's the limit, and you can become a great Pythonista!

If you’d like to learn more about this topic, check out Dataquest's interactive Introduction to Python course, and our Data Analyst in Python, and Data Scientist in Python paths that will help you become job-ready in around 6 months.

Mehdi Lotfinejad

About the author

Mehdi Lotfinejad

Mehdi is a Senior Data Engineer and Team Lead at ADA. He is a professional trainer who loves writing data analytics tutorials.