While older Macs come with a built-in version of Python, they are outdated and are no longer compatible with the latest applications. To run Python scripts on a Mac, you will have to manually install the latest version of Python on your system.
So here is a complete guide on installing Python on your Mac and getting started with the first script.
Do Macs Still Come With Python Pre-installed?
For many years, Macs came preinstalled with Python 2.7. However, this is no longer the case. While you can still download and install Python on your Mac, like any other program, Apple no longer includes it by default because Python 2.7 is no longer maintained.
Apple had given early notices of its removal from its operating system in the macOS Catalina release notes in 2019 before finally pulling the plug in macOS Monterey.
However, if your Mac version is between 10.8 to 12.3, there is a good chance that you have Python 2.7 pre-installed on your Mac. The best way to check is to simply try to run Python from the Terminal app in macOS.
How to check the version of Python pre-installed on your Mac
If you are running an older version of macOS, you can check which version of Python is available on your Mac by following these steps:
- Run the Terminal app on your Mac. To do this, go to the launchpad of your Mac and type hall in the search field. Alternatively, you can use Spotlight search to find it.
- Enter the following command to check the version of Python 2.7 and press Return:
If Python was previously installed, you will receive a response providing details about the version.
- If your Mac has multiple versions of Python, you can enter a command that specifies the version of Python. To check the version of Python 2.7, you can use the . file Python – version ordering. And for Python 3 you can use the following command:
How to install Python using the official installer
The easiest way to download the latest version of Python is through the official website. Here’s what you need to do to get started:
- Download the installer package from Python official website.
- Wait for the download to complete. Once done, double-click on the package to start the installation process. You can follow the onscreen instructions in the Python installer for this step.
- Once the installation is complete, the installer will automatically open the Python installation directory in a new Finder window.
Confirm your Python installation
You need to confirm that you have installed Python and IDLE correctly, the integrated development environment that comes with Python – just one of many vital tools for Python developers.
- In the Python installation directory, you will find a file named IDLE. Double-click on it to run it, and if the installation is successful, IDLE will open the Python shell.
- To make sure everything is running smoothly, enter a simple Python print command and run it in IDLE.
- Press the Return (or Enter) key, and your message will be printed on the interface if everything is working correctly.
Install the source code editor on your Mac
Now that you have Python installed on your Mac, you need to install a free code editor that can help you make programming faster and easier. Although the IDLE that comes as part of the Python package is a source code editor as well, and you can use it to get started, some of the better editors provide a much more efficient coding environment.
So, if you want a lightweight and powerful code editor, you’ll need something like Visual Studio Code. Here’s how to install it:
- You can download files Visual studio code Free from the official website.
- Once done, click on the downloaded file and extract its archived contents.
- Move VSCode to the application folder. Doing so makes it available in the macOS launchpad for easy access.
- Now, launch VSCode and create a new file by clicking New file…
- In order for Visual Studio Code to read your file as a Python script, you must create a file with the .Preparatory Year extension.
- Now, select a location to save your new Python script and click create a file.
- VSCode will now prompt you to install the Python extension. You can do this by clicking Installations button.
However, if you miss clicking the popup asking you to install the extension, or if you close it by mistake, you can still install the Python extension manually. You can do this by clicking Accessories Icon (four squares) on the left side of Visual Studio Code. Doing so brings up a list of the most popular Python extensions in the Visual Studio Marketplace. Next, search for Python and install the first extension that appears in the search results.
- After installing the Python extension, choose the Python interpreter by clicking Select the Python interpreter button and choose one recommended from the list.
Run your first Python script on a Mac
Getting this far in the process means you’ve successfully installed Python on your Mac and been able to set up a code editor. Now it’s time to run your first Python script on macOS. To do this, follow these instructions:
- Go back to the Python file you just created and enter any basic Python command like:
- You can compile and run the code by clicking the Run button in the upper right corner of Visual Studio Code.
- If your code is written correctly, you will get the output displayed in the built-in Terminal, which means that you have successfully run your Python program for the first time.
How to uninstall Python from your Mac
If you change your mind about using Python on your Mac and plan to switch to a different programming language, you can follow the steps below to remove Python from macOS.
Uninstall Python from Applications folder
- Go to Finder and click on the application folder.
- Move all installed Python folders to the Recycle Bin. To remove a single version, be sure to move only the files of that specific version to the Trash.
- A dialog box may appear asking for your permission to perform the deletion. Enter your Mac’s password to give Terminal permission to remove these files.
Uninstall Python Framework from /Library Directory
If you want to completely uninstall Python from your system, you will also need to remove the Python framework from the /Library directory. The Python framework contains the Python interpreter, standard library, and site packages folder.
Removing the Python framework will remove all of these files from your system. So, if you don’t remove these files, Python will still be installed on your Mac but it will not be usable.
- Hit Command + Spacebar to bring up Spotlight Search. Here, search for hall and release it.
- Enter the following command in Terminal and press Return (or Enter).
sudo rm -rf /Library/Frameworks/Python.framework
- Enter your Mac’s password to give permission to delete files.
This will delete all framework versions of Python installed on your Mac. However, if you only want to delete a specific version framework (such as version 3.8), you can use the following command:
sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.8
This will delete the framework for the selected version of Python from your Mac.
Getting started with Python on your Mac is easy
Although there are many ways to install Python on macOS, the most secure method is to use the official installer. However, you can also use a macOS package manager like Homebrew – an app that makes it easy to install and manage programs, dependencies, and other scripts on your computer.
The best part is that you can not only use Homebrew to install any version of Python but many other applications as well.