Fundamentals

How to Install Python for AI Projects on Mac

Install a clean, isolated Python environment for AI on macOS without touching the system Python. Covers Homebrew, python.org, pyenv, venv, and SDK setup.

This guide shows you how to install a clean, isolated Python for AI work on your Mac in under fifteen minutes, without touching the Python that macOS depends on. By the end you will have a versioned Python, a project-specific virtual environment (a private folder that keeps each project's packages separate), and the core AI libraries installed and verified.

macOS ships with its own Python, but it is there for the operating system's own tools. Installing AI packages on top of it can cause permission errors and, in the worst case, break system utilities. The fix is simple: install a separate Python and keep every project in its own environment. If you want the bigger picture on why isolation matters, the foundational concepts live in Python AI Fundamentals for Non-Developers.

Prerequisites

You only need a few things before you start:

  • macOS 12 or newer, on either Apple Silicon (M1/M2/M3) or an Intel chip. Both work; the only difference is where Homebrew installs.
  • The Terminal app, found in Applications → Utilities → Terminal. Every command below is typed there and run by pressing Return.
  • An internet connection for downloading Homebrew, Python, and the AI packages.
  • An OpenAI API key if you plan to call a model. You can create one in your OpenAI account dashboard. You do not need it to install anything; you only need it when you run code that talks to a model.

A quick reassurance about the commands: a line starting with $ is something you type, but you copy only the part after $. Lines that start with # in bash are comments and are ignored.

Step 1: Install Homebrew

Homebrew is a package manager (a tool that downloads and installs software for you) that keeps everything tidy in its own folder, so it never overwrites macOS system files. Most Mac developers use it, and it makes installing and updating Python a single command later on.

Paste this into Terminal and press Return:

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

The installer explains what it will do and asks for your Mac password to continue. When it finishes, it prints one or two lines telling you to add Homebrew to your shell. On Apple Silicon, Homebrew installs to /opt/homebrew, so run:

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

On Intel Macs, Homebrew installs to /usr/local and is already on your PATH (the list of folders your Mac searches for commands) once installation completes. Confirm Homebrew is working:

brew --version

If you would rather skip Homebrew entirely, jump to Step 2's python.org option — you can install Python without it.

Step 2: Install Python

You have two good ways to install Python. Pick one; you do not need both.

Option A — Homebrew (recommended for most people). This installs a specific Python version as a clearly named binary and makes future upgrades easy.

brew install python@3.11
python3.11 --version

You should see Python 3.11.x. Homebrew installs Python as the versioned command python3.11. Use that exact name when creating environments — do not rely on the bare python3, because it can point to an older release.

Option B — the python.org installer (recommended if you prefer a double-click installer). Go to python.org/downloads, download the latest macOS installer for Python 3.11 or newer, and double-click the .pkg file. Follow the prompts, then confirm in Terminal:

python3 --version

Either option gives you a Python that is safe to build AI projects on. Whichever you choose, never modify the system Python in /usr/bin.

There is a third route, pyenv, which is worth knowing about even though you probably do not need it yet. The tree below shows what each installer buys you and where all three paths meet again.

Choosing between Homebrew, the python.org installer and pyenv on a Mac A decision tree that starts at the choice of installer, branches into Homebrew, the python.org package and pyenv with the trade-off of each, and converges on the same next step of creating a virtual environment. Pick an installer three good paths Homebrew brew install python easy upgrades later python.org double-click .pkg version stays fixed pyenv brew install pyenv many versions at once Then create a venv python3.11 -m venv
All three installers land you in the same place, so pick on how you like to update software rather than on capability — the virtual environment step that follows is identical either way.

Step 3: Create a virtual environment

Never install AI packages directly into your main Python. Instead, create a virtual environment (often shortened to "venv") — a self-contained folder that holds one project's packages. This keeps projects from clashing and lets you delete a project cleanly by removing its folder.

Make a project folder, create the environment inside it, and activate it:

mkdir ~/ai-workspace && cd ~/ai-workspace
python3.11 -m venv .venv
source .venv/bin/activate

If you installed from python.org, use python3 in place of python3.11 on the second line. Once activated, your prompt shows (.venv) at the start of the line — that is your signal that the environment is on. To leave it later, type deactivate. For a deeper walkthrough of environments across operating systems, see Create a Python Virtual Environment for AI.

It helps to picture what now sits under your script. Your code runs on packages from the .venv, which was built from the Homebrew Python — and the Python macOS ships with is off to one side, doing its own job.

What sits underneath a Python AI script on a Mac A layered stack showing your script on top of a project virtual environment, which in turn sits on the Homebrew Python, next to a separate box for the macOS system Python that your project never uses. Your project stack Left alone Your script check.py and app.py Project .venv openai, httpx, dotenv Homebrew Python /opt/homebrew/bin System Python /usr/bin/python3 runs macOS tools never install here
Each layer only depends on the one below it, which is why deleting a project folder removes its packages cleanly and leaves both the Homebrew Python and the macOS system Python untouched.

Step 4: Install the core AI packages

With the environment active, upgrade pip (Python's package installer) and install the essentials. We use the official openai SDK and httpx (a modern library for making web requests) rather than older alternatives, plus python-dotenv for loading secrets safely.

pip install --upgrade pip
pip install openai httpx python-dotenv

Now store your API key in a .env file (a small text file holding secrets) in the project root:

OPENAI_API_KEY=sk-proj-your-key-here

Then add .env to .gitignore immediately, so your key is never committed to version control or shared:

echo ".env" >> .gitignore

That one line is the difference between a private key and a leaked one — never skip it. Here is the whole path your key takes: it moves from the file into your running script, and the .gitignore entry is what stops it going anywhere else.

How an API key travels from a .env file into your Python script A data-flow diagram: the .env file feeds load_dotenv, which makes the key available to the OpenAI client, while a second branch shows the same file listed in .gitignore so it is never committed. Your .env file OPENAI_API_KEY load_dotenv() reads .env into env OpenAI client os.getenv reads it Add to .gitignore one line, once Never committed safe to push code
The key only ever moves along the top row, from the file into memory and into the client; the bottom row is the guard that keeps the file itself out of version control.

Step 5: Verify the setup

First, a quick import check to confirm the packages load without path errors:

python -c "import openai, httpx; print('AI environment verified.')"

If this stops with ModuleNotFoundError: No module named 'openai', the packages went into a different Python than the one you just ran — Fix ModuleNotFoundError: No Module Named openai walks through finding which one.

If that prints the success message, run a tiny script to confirm everything fits together, including reading the key from .env. Save this as check.py in your project folder:

"""check.py — confirms Python, packages, and your .env key all work."""
import sys
from dotenv import load_dotenv
import os

load_dotenv()  # reads the .env file into environment variables

print(f"Python version: {sys.version.split()[0]}")

key = os.getenv("OPENAI_API_KEY")
if key and key.startswith("sk-"):
    print("API key loaded from .env: OK")
else:
    print("API key not found — check your .env file")

Run it:

python check.py

You should see your Python version and a confirmation that the key loaded. Your Mac is now ready for AI scripting. Writing longer scripts in TextEdit gets old fast, so this is a good moment to Choose a Code Editor for Python AI Work — most editors will detect the .venv folder you just made and use it automatically. To make your first real call, head to Understanding LLM APIs.

Key commands quick reference

CommandWhat it doesWhen you use it
brew install python@3.11Installs a versioned Python via HomebrewOnce, during setup
python3.11 -m venv .venvCreates a virtual environment named .venvOnce per new project
source .venv/bin/activateTurns the environment on (prompt shows (.venv))Every time you open the project
deactivateTurns the active environment offWhen you finish working
which pythonShows which Python is currently activeTo check the environment is on

Troubleshooting

  1. command not found: python — On macOS the installed command is usually python3, not python. Plain python only works inside an activated virtual environment. Either activate your environment with source .venv/bin/activate, or use python3 directly.
  2. brew: command not found after installing Homebrew — The shell setup line did not run. On Apple Silicon, run eval "$(/opt/homebrew/bin/brew shellenv)" again, then add it to ~/.zprofile so it loads in every new terminal window. Close and reopen Terminal to confirm.
  3. The wrong Python runs (multiple Pythons installed) — Run which python3 to see which one is active. If it points to /usr/bin/python3, that is the system Python; create and activate a virtual environment so your project uses its own copy instead. Inside an active .venv, which python should point to a path containing .venv.
  4. pip install fails with a permissions error — This almost always means no virtual environment is active, so pip is trying to write into a protected system folder. Check your prompt shows (.venv). If not, activate the environment first, then run the install again. Never use sudo pip install to force it.
  5. SSL: CERTIFICATE_VERIFY_FAILED on your first API call — This is common after a python.org install on macOS, because that installer ships its certificates as a separate step. Open Applications → Python 3.11 in Finder and double-click Install Certificates.command, then run your script again. Fix SSL: CERTIFICATE_VERIFY_FAILED in Python covers the other causes, including corporate networks.

When to use this vs. alternatives

  • Homebrew (this guide's main path) — Best if you want one tool to install and update Python, Git, and other utilities from the terminal, and you are comfortable running an occasional command. This is the right default for most creators and founders.
  • python.org installer — Best if you prefer a double-click installer over the terminal and want a version that never changes underneath you. A solid choice if you only need Python for one or two projects.
  • pyenv — Best if you expect to juggle several Python versions across different projects (for example, an older library that needs 3.10 alongside a newer one on 3.12). Install it with brew install pyenv. It is the most flexible option but adds setup overhead, so reach for it only once you actually need multiple versions.

Setting up on a different machine? Follow How to Install Python for AI on Windows for the Windows equivalent of every step here, or How to Install Python for AI on Linux if you are working on a Linux box or a remote server — the venv and .env steps are word-for-word the same on all three.

Back to Setting Up Python for AI.

Frequently asked questions

Can I use the Python that already comes with my Mac?

No. The Python that ships with macOS is reserved for system tools, and modifying it can break parts of the operating system. Install a separate Python with Homebrew or from python.org and use that for all your AI projects.

Do I need to install Python differently on an M1, M2, or M3 Mac?

The steps are the same, but Homebrew installs to /opt/homebrew on Apple Silicon instead of /usr/local on Intel Macs. As long as you run the shell setup line printed by the installer, everything works the same way afterward.

Should I install Python with Homebrew or download it from python.org?

Homebrew is best if you want one tool to manage everything and update easily from the terminal. The python.org installer is best if you prefer a double-click installer and a fixed version that never changes underneath you.

Why do I keep getting 'command not found: python'?

On a Mac the command is usually python3, not python. Inside an activated virtual environment, plain python works because the environment creates that alias for you.

How do I keep my OpenAI API key safe on my Mac?

Store it in a .env file in your project folder and load it at runtime, never paste it directly into your code. Always add .env to your .gitignore so the key is never committed or shared.