Skip to content

🐍 Python Installation Guide for macOS

Updated: at 12:45 PM

Table of Contents

Open Table of Contents

🚀 Overview

This guide will walk you through installing Python on your Mac using Homebrew, pyenv, and Poetry. This setup is recommended for development in our project.

📋 Prerequisites

  • A MacBook running macOS

  • Administrator access to your machine

  • Internet connection

🍺 Step 1: Install Homebrew

  1. Open Terminal (Applications > Utilities > Terminal)

  2. Install Homebrew:


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

  1. Follow the prompts in Terminal

  2. Add Homebrew to your PATH:

To determine which shell you’re using, run:


echo $SHELL

  • If you’re using Zsh (default on newer macOS versions):


echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc

  • If you’re using Bash:


echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile

After adding the line, run:


source ~/.zshrc # for Zsh

# or

source ~/.bash_profile # for Bash

  1. Verify installation:


brew --version

🔧 Step 2: Install pyenv

  1. Install pyenv:


brew install pyenv

  1. Add pyenv to your shell:

For Zsh:


echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc

echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc

echo 'eval "$(pyenv init -)"' >> ~/.zshrc

For Bash:


echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile

echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

  1. Restart your terminal or run:


source ~/.zshrc # for Zsh

# or

source ~/.bash_profile # for Bash

🐍 Step 3: Install Python

  1. Install Python (e.g., 3.11.2):


pyenv install 3.11.2

  1. Set as global version:


pyenv global 3.11.2

  1. Verify installation:


python --version

📦 Step 4: Install Poetry

  1. Install Poetry:


curl -sSL https://install.python-poetry.org | python3 -

  1. Add Poetry to your PATH:

For Zsh:


echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc

source ~/.zshrc

For Bash:


echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bash_profile

source ~/.bash_profile

  1. Verify installation:


poetry --version

  1. Configure Poetry:


poetry config virtualenvs.in-project true

This setting ensures that Poetry creates virtual environments within your project directory.

🚀 Step 5: Create a New Project

  1. Create a project directory:


mkdir my_project

cd my_project

  1. Initialize a new Poetry project:


poetry init

  1. Add dependencies:


poetry add package_name

  1. Create a virtual environment and install dependencies:


poetry install

  1. Activate the virtual environment:


poetry shell

🖥️ IDE Setup

While our team primarily uses PyCharm, this guide is IDE-agnostic. Here’s how to set up your preferred IDE with Poetry:

PyCharm

  1. Open your project in PyCharm

  2. Go to Preferences/Settings > Project > Python Interpreter

  3. Click the gear icon and select “Add”

  4. Choose “Poetry Environment”

  5. Select the existing environment in your project folder (usually .venv)

Visual Studio Code

  1. Install the Python extension

  2. Open your project folder

  3. Press Cmd+Shift+P and type “Python: Select Interpreter”

  4. Choose the Poetry environment (usually .venv/bin/python)

Cursor.sh

  1. Open your project in Cursor

  2. Go to Settings > Python > Python Path

  3. Set it to the path of your Poetry virtual environment (e.g., /path/to/your/project/.venv/bin/python)

Remember to always activate your Poetry environment before running your project:


poetry shell

🔄 Maintenance and Updates

  • Update pyenv: brew upgrade pyenv

  • Update Poetry: poetry self update

  • Update Python: pyenv install <new_version> && pyenv global <new_version>

  • Update project dependencies: poetry update

For more information, refer to:

This guide combines information from multiple sources, including Stack Overflow discussions and Dataquest’s Python Installation Tutorial.