Python Programming on MacOS: Get Started QUICK and EASY!

Published Categorized as Computing, Mac, Programming, Python
Beginners Guide To Python Programming on MacOS

Computer programming might not be the first thing that comes to mind when you think of Apple products.

Therefore you may not already know that MacOS is a fantastic platform for developers!

If you want to get started with Python programming and you have a Mac then you are in luck! You already have everything you need to get started in Python programming.

And that’s not even the best news!

Python is already one of the easiest languages for beginner programmers to learn and MacOS is probably one of the best environments to learn it quickly.

Ok, ready to begin? Great! Let’s get into it.

Table of Contents

  1. Prerequisite
  2. Can I Learn Python on a Mac?
  3. Is MacOS Good for Python?
    1. Is Python Better on Mac or Windows?
    2. Does Python Work on M1 Mac?
  4. How To Install Python on MacOS
    1. How To Install Python on MacOS Using the Homebrew Package Manager
  5. How To Use the Python Interpreter on MacOS
    1. Python as a MacOS Calculator
  6. How To Write a Python Script on MacOS
  7. What’s Next?

Prerequisite

In this article we will get you up and running with Python on MacOS in no time!

Our goal is to get your first Python program up and running as quickly and easily as possible.

We won’t dive too deeply into the details about what Python is and I will assume you have already reached the decision that you want to learn it, hence ending up here!

However there is an excellent introductory article over on the official site that I would recommend reading.

Aside from that the only thing you will need for this tutorial is your Mac!

The software that you will need is all free of charge. We will cover exactly what you need and where to get it later in the tutorial.

I will begin by answering some frequently asked questions about Python on Mac.

For those in a hurry, you can skip it and either jump to installing Python on MacOS or jump directly into some coding!

Can I Learn Python on a Mac?

You can easily learn Python on a Mac simply by using the Terminal program built into MacOS with a compatible copy of Python installed.

Python can be dynamically typed directly into the MacOS Terminal, which will give you results right before your eyes! More on that later.

However you can also write Python code in a text editor and save it as a script, which can be executed when required or compiled into an executable program.

Python scripts are simply text-formatted files stored on your computer.

You can create them directly in the Terminal using one of the popular Linux Terminal text-editors, see this article for further information.

However I would recommend pairing the MacOS Terminal with a good text editor that you can use to write your Python programs.

TextMate as a really good text editor for coding on the Mac. It is easy to use, pretty to look at and it is completely free to download.

TextMate for MacOS

Is MacOS Good for Python?

MacOS is certainly a good choice for Python coding due to its similarities with Linux and roots in UNIX.

Many developers choose Linux for their primary operating system for a whole host of reasons. It could be said that many of those same reasons apply to MacOS too.

Whether or not MacOS is a better choice than Linux is solely personal preference and depends on what you want to do with your computer.

However if you are an Apple fan and you prefer MacOS for everything else you do with your computer, MacOS is without doubt going to be the best choice for you to learn Python.

Is Python Better on Mac or Windows?

Python is pretty universal and many people report that it works equally as well on Windows as it does with MacOS, or Linux.

One of the coolest things about Python is the level of cross-compatibility.

You can even code Python directly on Raspberry Pi, or even smaller microcontrollers like ESP8266.

Therefore Python is not necessarily better on Mac, Windows or any other platform.

Python is highly cross-compatible and you should choose your preferred platform based upon other reasons, such as what OS you prefer overall or what your budget constraints are.

Therefore;

Windows is better if you already own a PC.

MacOS is better if you already own a Mac.

Does Python Work on M1 Mac?

Python now works natively on Mac computers using Apple Silicon, such as the M1 chip, since version 3.9.

Prior to this it was only possible to run the x86 architecture version of Python using Rosetta 2.

This meant that Python could still be used but with an approximate 20% to 30% decrease in performance due to the emulation required.

However since the update it has been possible to run Python natively and without a detriment to the performance.

All of the tutorials that I have published since April 2022 have been written and completed using an M1 MacBook Pro, including this one!

How To Install Python on MacOS

The best way to install Python on MacOS is by installing the Command Line developer Tools:

  1. Open the MacOS Terminal application found in the applications folder.
  2. Type the command python3 --version in order to confirm that Python is not already installed.
  3. When prompted, install the command line developers tools.
  4. Type python3 into the Terminal to launch the Python prompt.

The Command Line Developer Tools are a part of Xcode and include a series of useful developer tools, including Python.

As many of these tools will be useful at some point and some of them necessary for developing via the command line, installing the Command Line Developer Tools would be the most recommended way to install Python on MacOS.

How To Install Python on MacOS Using the Homebrew Package Manager

You can also install Python on MacOS using Homebrew, a third-party package manager for MacOS:

  1. Visit the Homebrew website and copy the installation command from the front page.
  2. Open the MacOS Terminal application found in the applications folder.
  3. Paste the Homebrew installation command into the terminal and press enter.
  4. Insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file:
    • export PATH="/usr/local/opt/python/libexec/bin:$PATH"
  5. Once installation is complete, install Python using the command: brew install python.

How To Use the Python Interpreter on MacOS

So, you got your Mac, you got Python installed… finally lets get to some coding!

We will begin simply by using the MacOS Terminal in order to get an introduction to the Python Interpreter.

One cool thing about Python is that it has an interactive interpreter. Here’s how you take Python for a little test drive…

Go ahead and open the MacOS Terminal application found in the applications folder if you don’t already have it open. Then enter the following command:

python3

This should take you to the Python prompt. Here you should be presented with the Python version that you are running followed by the >>> prompt.

Ok so lets try some Python! Let’s enter the following Python command into the prompt and press enter.

print('hello world')

The function print() will print the argument passed to it on the screen, in this case our Terminal window.

The argument that we passed to the function was hello world, which we can see printed in the Terminal window. Awesome!

Note that if you wish to clear the contents of the interpreter screen in the Terminal window, the easiest way is to press control + L.

Python as a MacOS Calculator

Ok let’s try some more simple Python commands. Another little example that we can test is using Python as a calculator. The syntax is very straightforward, we can try something like:

3+5

The output from the interpreter will be the answer to this simple calculation, 8.

All of the main arithmetic symbols work, so you can try far more complex calculations.

You can also use brackets in order to change the order of operation.

How To Write a Python Script on MacOS

So far we have seen how the Python interpreter works when in interactive mode. This is a useful tool, but how to we actually write programs?

We can use what we have learned so far to create our first Python script.

What on earth is a Python script?

A Python script is a group of instructions included in a file that are intended to be run similarly to a program.

The cool thing about a Python script is that it can be executed whilst still existing as a human-readable script. You do not need to compile it first.

Let’s take a look at an example:

# Get user input and store it in the variable 'object'
object = input('What object do you wish to name? ')

# Generate a silly McName and store it in the variable 'mc_name'
mc_name = object + 'y Mc' + object + 'face'

# Print the McName in the Terminal window
print('You should name your object ' + mc_name)

Next, go ahead and open up your prefered text editor. If you don’t have a text editor for coding yet, I would recommend downloading TextMate.

Enter the code into the text editor and then save the file as McName_Generator.py on your desktop.

We can execute this script from the MacOS Terminal prompt.

If you are still at the Python interpreter prompt >>> then type exit() and press enter. This will take you back to the main terminal prompt.

Once at the main Terminal prompt, first make sure you are pointing the command line to the desktop:

cd ~/Desktop

Next, type the following command to launch the Python script:

python3 McName_Generator.py

We have now successfully launched our first Python script on MacOS. Awesome!

What’s Next?

In this tutorial we have learned how to install Python on a Mac and successfully execute some basic code using the Python interpreter.

The next tutorial in this series will take a look at some more interesting commands, where the end goal is to create a simple GUI application.

Did you know that you can also run Python on tiny little microcontrollers too?! Check out this article for more info!