Skip to main content

How to Install Python on macOS

Written by:

February 18, 2025

0 mins read

Python is a high-level, interpreted, general-purpose dynamic programming language focused on code readability. The syntax in Python helps programmers code in fewer steps compared to Java or C++. The language was founded in the late 1980s, and its implementation started in December 1989 by Guido van Rossum. It is currently one of the most popular and influential languages in software development. To ensure secure and high-quality Python code, developers can use Snyk's Python Code Checker to detect vulnerabilities and code issues automatically. This tool helps identify security risks, maintain best coding practices, and improve overall code health. By integrating Snyk’s Python code checker's scanning capabilities, developers can write safer Python applications with confidence.

The importance of Python 

Python has been an important part of Google since the company's beginning and is recognized for its easy readability and great design principles. As Python's creator Guido van Rossum said in 2007, "Python is an experiment in how much freedom programmers need. Too much freedom and nobody can read another's code; too little and expressiveness is endangered."

# Python code is easy to read and understand
def greet(name):
    print(f"Hello, {name}!")

greet("Python Developer")

Python 2.x vs Python 3.x

Python 2.x is a legacy version of the programming language, while Python 3.x is the current and future-focused standard. The main differences between these versions lie in their syntax, print function usage, integer division, Unicode handling, and range function behavior.

Given that Python 2.x is no longer actively supported, developers are strongly encouraged to migrate their projects to Python 3.x. This ensures access to the latest features, performance improvements, and a more vibrant community of developers

# print function in Python 2.x
print "Hello, Python 2.x Developer!"

# print function in Python 3.x
print("Hello, Python 3.x Developer!")

Python is a powerful and influential language that has a significant role in the software development industry. Its simplicity and readability make it a preferred choice for many developers. Whether you are new to programming or an experienced developer, Python can accommodate your needs and skill levels.

Why Python installation can be tricky on macOS

Installing Python on macOS can sometimes be a daunting task. In this section, we will explore some of the common challenges you might encounter when installing Python on macOS, the issues with macOS's system Python, and the need for different Python versions for different projects.

Common issues developers face when installing Python on macOS

One common problem when installing Python on macOS is version management. macOS comes with a pre-installed version of Python, but this version is typically outdated and used by the system for various tasks. Therefore, updating it might cause system issues.

Another challenge is package management. Python has a rich ecosystem of libraries and frameworks that can be installed using pip. However, managing these packages across different projects and Python versions can be quite cumbersome. Here's an example of how package management can become tricky:

$ python3 -m pip install requests

In the code above, we're installing the 'requests' library. But this library will be installed for the specific Python version that 'python3' points to. Managing such installations can become confusing if you have multiple Python versions installed.

The problem with macOS system Python

As mentioned earlier, macOS comes with a pre-installed Python version. This version, often called "system Python," is used by the operating system to perform various tasks. Updating or modifying this Python version can lead to system instability or even break certain functionalities.

The Python system is usually not the latest version, so as a developer, you may need to work with more recent versions of Python. This means you have to install them alongside the Python system, which can lead to confusion, especially when ensuring the right Python version is used for the right task.

The need for different Python versions for different projects

Each project you’re working on may require a different Python version. One project might run perfectly on Python 3.6, while another might require features only available in Python 3.8 or later.

Managing these different Python versions can be tricky. For instance, when you run a Python script, you need to ensure that it's being executed with the correct Python version. This can be even more complicated when dealing with different library versions across projects.

Here's an example of how you might specify the Python version for a project:

$ python3.6 my_script.py

In the code above, we're explicitly using Python 3.6 to run my_script.py. But imagine having to do this for every script or every time you work on a different project. You can see how this can quickly 

Different ways to install Python on macOS

We’ll look at different ways to install Python on your macOS.

Manual installation from python.org

The first step to manually installing Python on macOS is to download the installer package from the official Python website, Python.org. This is the primary source for the most recent versions of Python.

To download the Python installer package, follow these steps:

  1. Open your web browser and navigate to the Python.org downloads page.

  2. The website should automatically detect your operating system and suggest the latest Python version to download. If not, manually select 'macOS' and download the latest Python version.

  3. Click the download button for the latest Python version. The file will typically have a .pkg extension, which stands for 'package'.

After downloading, you should have a Python .pkg file in your Downloads folder or wherever your browser typically saves files.

Running the installer

Once you've downloaded the Python installer package, the next step is to run the installer.

  1. Navigate to your Downloads folder and double-click the .pkg file.

  2. This will start the Python Installer. Follow the instructions provided by the installer, making sure to agree to the terms and conditions and confirm the installation location.

  3. The installer will ask for your password before it begins to install Python on your system.

Verifying the installation

After the installation process is complete, verify that Python was correctly installed on your system.

To verify your Python installation, you need to use the Terminal:

  1. Open Terminal (you can find it in the Utilities folder within your Applications folder or by searching for it in Spotlight).

  2. In the Terminal, type the following command and press Enter:

python3 --version

Your Terminal should print out the version of Python that you just installed, something like:

Python 3.9.6

If your Terminal prints out a Python version, Python was successfully installed on your system.

Updating Python on MacOS

If you need to update Python to a newer version in the future, you can follow the same process: download the new Python installer package from Python.org and run the installer.

However, it's important to note that Python does not overwrite previous versions when you install a new one, as different projects may require different Python versions. Therefore, you will need to specify the Python version when running your scripts. For example, to run a script with Python 3.9, you would use the python3.9 command in your Terminal.

Installation using Homebrew

Homebrew is an open-source software package management system that simplifies software installation on Apple's Mac OS and Linux. The name is intended to suggest the idea of building software on the Mac, depending on the user's taste. Originally written by Max Howell, the package manager has gained popularity in the macOS community as an alternative to the Mac App Store.

Using Homebrew to install Python

Homebrew simplifies the installation process and manages upgrades and updates for you, making it an excellent tool for developers and DevOps.

Here's a step-by-step guide on how to install Python on macOS using Homebrew:

  1. First, you need to open the Terminal app. You can do this by searching for it in Spotlight or accessing it through the Utilities folder located in the Applications folder.

  2. If you haven't installed Homebrew yet, you can install it by running the command below in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

3. After Homebrew has been installed, you can install Python by running the following command in your terminal:

brew install python

This command will download and install Python on your system.

Verifying the installation

After the installation is complete, you can verify that Python was correctly installed by running the following command in your terminal:

python3 --version

This command should output the version of Python installed, verifying that Python was correctly installed on your system.

Keep in mind that Python 2 comes pre-installed on Mac and is used by the operating system. Therefore, when you install Python via Homebrew or other methods, you will be installing Python 3, and you will need to use the python3 command to execute your scripts.

Installation using Pyenv

Pyenv is a simple, powerful, and cross-platform tool that enables you to manage multiple Python versions in the same system without conflicts. It provides a way to easily switch between different versions, set a version as the default for your system, and even specify the version on a per-project basis. This is extremely helpful when working on multiple projects that each require different Python versions.

How to install Pyenv

Before you install Pyenv, ensure you have the necessary dependencies. On macOS, you'll need Homebrew. If you don't already have it installed, you can install it using this command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once Homebrew is installed, you can install Pyenv with the following command:

brew install pyenv

Using Pyenv to manage multiple Python versions

With Pyenv installed, you can now install multiple versions of Python. To do so, use the pyenv install command followed by the version number. For example, to install Python 3.9.1, you would use:

1pyenv install 3.9.1

You can list all the Python versions available for installation with the pyenv install --list command. To switch between installed versions, use the pyenv global command followed by the version number like this:

pyenv global 3.9.1

To set a Python version for a specific project, navigate to the project's directory and use the pyenv local command like this:

cd my-project
pyenv local 3.8.6

To confirm that Pyenv is managing your Python versions as expected, you can use the pyenv versions command. This will list all installed Python versions with an asterisk (*) next to the current active version. You can also verify the Python version used in a specific project by navigating to the project directory and using the python --version command.

In conclusion, Pyenv is a valuable tool for any developer working with Python. It simplifies the management of multiple Python versions, allowing you to focus on coding instead of configuration.

Importance of virtual environments in Python development

Python, is a versatile and widely used programming language that has an extensive ecosystem of modules and packages. As you navigate this ecosystem, it's important to understand the role of virtual environments. In this section, we’ll explore what virtual environments are, why developers need them, and different tools for creating Python virtual environments.

What are virtual environments?

A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus several additional packages. It is a tool that helps keep dependencies used by different projects separate by creating isolated spaces for them. This means that each of your projects can have its own dependencies, regardless of what dependencies every other project has.

Let's take a look at an example of how to create a virtual environment using Python's built-in venv module:

$ python3 -m venv /path/to/new/virtual/environment

In the above command, /path/to/new/virtual/environment can be replaced with the destination of your choice. This command creates a directory if it doesn't already exist and sets up a new virtual environment at the specified location.

Why developers need virtual environments

Working on multiple Python projects may require different versions of Python and Python packages. Installing everything globally could lead to version conflicts and other issues. This is where virtual environments come in.

Virtual environments allow developers to work on multiple Python projects without worrying about interdependencies. They provide an isolated space where you can install Python and other packages, ensuring that each of your projects has its own set of dependencies that do not interfere with each other.

Additionally, virtual environments help ensure that your projects are reproducible. This is extremely important when you're working on a team and need to share your code with others.

Using venv to create Python virtual environments

venv is a Python module that creates virtual environments. A virtual environment is an isolated runtime environment where you can install packages without affecting the global Python setup. This isolation prevents conflicts between packages and allows you to install different versions of the same package in different environments. This makes venv an essential tool for developers and DevOps specialists dealing with Python applications.

How to create a virtual environment using venv

Creating a virtual environment using venv is straightforward. In your terminal, navigate to your project directory and run the following command:

python3 -m venv myenv

This command will create a new directory named myenv in your current directory. This new directory houses the virtual environment and contains the Python interpreter, the standard library, and various supporting files.

How to activate and use the virtual environment

Once you've created your virtual environment, you'll need to activate it. Activating the environment adjusts your PATH so that the python command runs the version from your virtual environment, rather than the global Python interpreter.

In macOS, you can activate the virtual environment using the following command:

1source myenv/bin/activate

Upon activation, your terminal prompt will change to indicate the active environment. Your terminal should look something like this:

(myenv) $

You can now install packages in this environment without affecting your global Python setup.

Installing Python 3 in the virtual environment

Given that the venv module comes with Python 3.3 and later, Python 3 is automatically installed in the virtual environment when you create it. However, you can specify a different Python version when creating the environment. For example, to create an environment with Python 3.7, you would use the following command:

python3.7 -m venv myenv

Verifying the installation

To confirm that Python 3 is installed in your virtual environment, you can use the following command:

python --version

This command should return the Python version that is installed in your environment.

Remember, using venv to create and manage virtual environments is an essential skill in Python development. It's an effective way to manage project dependencies and ensure the consistency and reliability of your projects. Happy coding!

Additional tools for creating Python virtual environments

1. virtualenv: This is a very popular tool that creates isolated Python environments. It's older than venv and has more features.
Usage:

$ pip install virtualenv
$ virtualenv myenv

2. pipenv: This tool aims to bring the best of all packaging worlds to the Python world. It harnesses Pipfile, pip, and virtualenv into one single command.
Usage:

$ pip install pipenv
$ pipenv --three

Setting up a local Python development environment in a Docker container

In this section, we will cover how to setup a local Python development environment using Docker. We'll explain what Docker is, why it's beneficial for Python development, how to create a Dockerfile, how to build and run a Python application in a Docker container, and how to secure your Python Docker applications with Snyk.

What is Docker

Docker is an open-source platform that automates the deployment, scaling, and management of applications. It encapsulates software into standardized units known as containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can ensure consistent environments from development to production.

Why use Docker for Python development

There are several reasons why Docker is beneficial for Python development:

  • Consistency: Docker containers ensure that your application runs exactly the same way, no matter where it's deployed.

  • Isolation: Docker provides isolation, meaning that dependencies of different projects are kept separate. This makes it easy to manage dependencies and avoid conflicts between projects.

  • Reproducibility: Docker containers are reproducible, meaning you can easily share them with others and they will get the exact same environment.

Creating a Dockerfile for a Python application

A Dockerfile is a script that contains collections of commands and instructions to create a Docker image. Here's a basic example of a Dockerfile for a Python application:

FROM python:3.8

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "./your-python-script.py"]

In this Dockerfile, we start with a Python 3.8 image. We then copy the requirements.txt file into the Docker image and install the Python dependencies. After that, we copy the rest of the code into the Docker image and specify the command to run the application.

Building and running a Python application in a Docker container

After creating a Dockerfile, you can build your Docker image using the docker build command:

docker build -t your-python-app .

This command builds a Docker image from your Dockerfile and tags it with the name 'your-python-app'.

To run the application in a Docker container, you can use the docker run command:

docker run -p 8000:8000 your-python-app

This command runs your application inside a Docker container and maps the container's port 8000 to your machine's port 8000.

How to keep your Python Docker applications secure with Snyk

Snyk is a tool that helps to find and fix security vulnerabilities in your Docker images. It integrates with your CI/CD pipeline to ensure that your Docker images are secure before they get deployed.

To use Snyk, you first need to install it:

npm install -g snyk

Then, you can test your Docker image for vulnerabilities:

snyk test --docker your-python-app:latest --file=Dockerfile

This command scans your Docker image and Dockerfile for vulnerabilities. If any vulnerabilities are found, Snyk provides detailed information about them and gives recommendations on how to fix them.

References

Installing Python and setting up a conducive development environment can be a complex task, especially when it comes to managing different versions of Python and isolating your project dependencies. Here are some useful references that will guide you through the process, and help you deepen your understanding of Python, Homebrew, Pyenv, venv, and Docker.

Python documentation

The official Python documentation provides a comprehensive guide on Python installation as well as a deep dive into its rich ecosystem. Whether you are a beginner or an experienced developer, the Python documentation is an indispensable resource.

Link: Official Python Documentation

Play Fetch the Flag

Test your security skills in our CTF event on February 27, from 9 am - 9 pm ET.