How to install Python on Mac and run the first script

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:

  1. 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.
  2. Enter the following command to check the version of Python 2.7 and press Return:
    Python 

    If Python was previously installed, you will receive a response providing details about the version.

  3. 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:
    python3 

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:

  1. Download the installer package from Python official website.

  2. 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.

  3. 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.

  1. 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.

  2. To make sure everything is running smoothly, enter a simple Python print command and run it in IDLE.
    print('Hello, world!')
  3. 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:

  1. You can download files Visual studio code Free from the official website.

  2. Once done, click on the downloaded file and extract its archived contents.
  3. Move VSCode to the application folder. Doing so makes it available in the macOS launchpad for easy access.
  4. Now, launch VSCode and create a new file by clicking New file…

  5. In order for Visual Studio Code to read your file as a Python script, you must create a file with the .Preparatory Year extension.

  6. Now, select a location to save your new Python script and click create a file.

  7. 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.

  8. 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:

  1. Go back to the Python file you just created and enter any basic Python command like:
    print('Hello, world!')
  2. You can compile and run the code by clicking the Run button in the upper right corner of Visual Studio Code.

  3. 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

  1. Go to Finder and click on the application folder.
  2. 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.

  3. 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.

  1. Hit Command + Spacebar to bring up Spotlight Search. Here, search for hall and release it.
  2. Enter the following command in Terminal and press Return (or Enter).
    sudo rm -rf /Library/Frameworks/Python.framework

  3. 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.

(Visited 1 times, 1 visits today)

Related posts