In this digital age, having access to a powerful programming language like Python is essential for unleashing your creativity in development. Python allows users to interpret statements in several operating systems (OSes), but how do you install Python on macOS devices?
In this tutorial, you will learn two ways to install Python on macOS, so you can later delve into building websites, machine learning, and software testing.
Keep reading to take your project to new heights with Python!
This tutorial comprises hands-on demonstrations. Ensure you have the following in place to follow along with this tutorial:
- A device running macOS 10.8 or later – This tutorial uses a Mac computer running macOS 11.7 (Big Sur).
- Homebrew installed on your macOS device.
Enforcing Homebrew to Install Python on macOS
The quickest way to install Python on your macOS device is via Homebrew, a popular package manager. Homebrew simplifies the installation and management of various software packages, including Python.
To install Python on your macOS device via Homebrew, follow these steps:
1. Open your terminal, and run the below brew
command to update
Homebrew. This command fetches the latest information about available software packages and their versions.
brew update
2. Next, execute the following command to install
python
on your device.
brew install python
3. Lastly, run the following python3
command to verify that Python is installed on your device.
python3 --version
You will see the currently installed Python version number (3.11.4) on your macOS device.
Installing Python on macOS via the Official Installer
If you would rather not install Python via Homebrew, that is fine; not all people like to use Homebrew. But worry not. Another way you can install Python on your macOS device is via the official installer from the Python website.
Installing via the official installer is slightly more complicated and requires you to go through multiple steps. But on a good note, this method allows you always to get the latest version of Python, and take advantage of the latest features, improvements, and security updates.
1. Open your favorite web browser, and visit the official Python download page for macOS.
2. Next, locate the latest version of Python 3, which, at this time of writing, is Python 3.12.0rc1, and click the download link under it to download the installer.
3. Once the download completes, double-click on the installer to initialize the installation process.
4. Now, click Continue in the installer window to continue with the installation.
5. Read the displayed information and click Continue to continue.
6. Read through the software license agreement, and click Continue.
7. When prompted, click Agree to accept the software license agreement and continue with the installation.
8. Next, click Continue to perform installing Python on your macOS device.
If you are wondering, you cannot select the installation destination, as it will automatically be installed by default.
9. Provide your user password when prompted, and click Install Software to authenticate the installation.
Wait for the installation to complete.
10. Once the installation completes, click Close to finalize and close the installer.
At this point, you have successfully installed the latest version of Python on your macOS device.
11. Open the Application folder, and you will see the newly-installed Python application.
12. Ultimately, run the below command in your terminal to check the version of Python installed on your macOS device.
python3 --version
Installing the PIP Package Manager
Although Python comes with many useful packages for development, you may want to install additional ones, like the PIP package manager. PIP allows you to seamlessly install and manage various additional packages to enhance your development environment.
1. Execute the following python3
command to install your system’s PIP package manager (ensurepip
).
python3 -m ensurepip
2. Next, run the below command to --upgrade
PIP (ensurepip
) to the latest version
python3 -m ensurepip --upgrade
3. Now, run the pip3
command below and check the installed PIP version on your system.
pip3 --version
The latest version at this time of writing is pip 23.2.1, as shown below.
Testing the Python Installation
With Python wholly installed on your system, how do you check your Python installation works correctly? The best way to test your Python installation is to run small programs.
In this example, you will launch Python in interactive mode, which allows you to run Python commands and instantly observe their outcomes directly. This capability provides an efficient means to validate your installation.
To test your Python installation, follow the steps below:
1. Execute the following python3
command, without any parameters or options, to initiate the Python interpreter in interactive mode.
python3
You will see your prompt changes to >>>, as shown below, which indicates the interpreter is ready to accept commands.
2. Next, run the below command to print
a Hello world
message directly in the interactive mode.
print('Hello world')
If everything works correctly, you will see the following output.
3. Finally, run the exit()
command below to exit the interactive mode and return to your default command prompt.
Conclusion
This tutorial has guided you through installing Python on your macOS device. You have also tested your installation works by launching Python in interactive mode and printing a simple message.
What you have seen in this tutorial is just the tip of the iceberg. Python is cable of doing much more as it supports program modularity and code reuse. Python has become enticing for Rapid Application Development as a high-level programming language with dynamic semantics.
Now, why not explore more of Python’s prowess? Perhaps start running Python scripts with Python and feel its automation power first-hand?