Easy Python 3 Installation on Mac

Installing Python using Homebrew

How to Install Python 3 on Mac Using Homebrew

Step 1: Install Homebrew

If you haven't installed Homebrew, open Terminal and paste the following command:

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

Hit Return and wait for the installation to complete.

Step 2: Add Homebrew to Your PATH

Add Homebrew to your PATH by adding the appropriate line below to your ~/.profile or ~/.bash_profile file:

For macOS 10.12 (Sierra) or older:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH

For newer versions:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Step 3: Install Python 3 with Homebrew

In Terminal, run:

brew install python3

Homebrew will download and install Python 3.

Step 4: Verify Installation

Check the Python version:

python3 --version

If you see the version number, Python 3 is installed and ready to use.

Bonus Tip: Using Pip

Pip, Python's package manager, comes with Python 3 when installed via Homebrew. Install Python packages using the pip3 command to ensure you're working with Python 3 packages.

A realistic image of a person using a MacBook and typing commands in the Terminal to install Python 3 with Homebrew, showcasing the step-by-step process described in the text.

Configuring the PATH environment

Setting Python 3 as Default

  1. Open Terminal
  2. Locate Your Profile File: Depending on your macOS version, you'll be using either .bash_profile or .zshrc in your Home directory. Create the file if it doesn't exist.
  3. Edit the File: Open your profile file in a text editor.
  4. Add the Alias: Type alias python='python3' to set Python 3 as the default when 'python' is invoked.
  5. Save and Close: Save your changes and close the editor.
  6. Activate Changes: In Terminal, type source ~/.bash_profile or source ~/.zshrc, depending on which file you edited.
  7. Test: Type python --version in Terminal. You should see Python 3's version number.

By following these steps, you've set Python 3 as your default Python version. This change simplifies running Python scripts and ensures that you're using the Python version installed on your system.1

A realistic image of a person working on a Mac laptop in a coding environment, with Terminal open and Python 3 set as the default language. The screen displays Python 3's version number.

Working with Virtual Environments

Understanding Virtual Environments

A virtual environment is an isolated setup for your Python project, allowing it to have its own libraries and Python version separate from other projects. This separation prevents conflicts between different projects' dependencies and enables reproducibility.

Creating Your First Virtual Environment

  1. Open Terminal and navigate to your project directory: cd path/to/your-project
  2. Run the following command, replacing myprojectenv with your desired name: python3 -m venv myprojectenv

Activating Your Virtual Environment

  1. Navigate into the virtual environment folder: cd myprojectenv
  2. Activate the virtual environment: source bin/activate
    The command prompt will change to show the active environment.

Working in Your Virtual Environment

With the virtual environment active, you can install specific package versions: pip install request==2.19.1
Your main system remains unaffected by the packages installed in the virtual environment.

Deactivating the Virtual Environment

Type deactivate to exit the virtual environment. The command prompt will return to its normal state.

Virtual environments allow you to manage multiple Python projects with different requirements without interference.2 Use them to keep your projects organized and functional.

A realistic image showcasing a person working on a Mac computer in a terminal window, setting up and managing virtual environments for Python projects. The screen displays code snippets and terminal commands related to creating and activating virtual environments.
  1. Matthes E. Python Crash Course: A Hands-On, Project-Based Introduction to Programming. 2nd ed. No Starch Press; 2019.
  2. Sweigart A. Automate the Boring Stuff with Python: Practical Programming for Total Beginners. 2nd ed. No Starch Press; 2020.

Writio – The AI content writer that effortlessly elevates your blog or website. This article was written by Writio.

Leave a Reply