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.
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.
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.
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 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. To make your first real call, head to Understanding LLM APIs.
Key commands quick reference
| Command | What it does | When you use it |
|---|---|---|
brew install python@3.11 | Installs a versioned Python via Homebrew | Once, during setup |
python3.11 -m venv .venv | Creates a virtual environment named .venv | Once per new project |
source .venv/bin/activate | Turns the environment on (prompt shows (.venv)) | Every time you open the project |
deactivate | Turns the active environment off | When you finish working |
which python | Shows which Python is currently active | To check the environment is on |
Troubleshooting
command not found: python— On macOS the installed command is usuallypython3, notpython. Plainpythononly works inside an activated virtual environment. Either activate your environment withsource .venv/bin/activate, or usepython3directly.brew: command not foundafter installing Homebrew — The shell setup line did not run. On Apple Silicon, runeval "$(/opt/homebrew/bin/brew shellenv)"again, then add it to~/.zprofileso it loads in every new terminal window. Close and reopen Terminal to confirm.- The wrong Python runs (multiple Pythons installed) — Run
which python3to 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 pythonshould point to a path containing.venv. pip installfails 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 usesudo pip installto force it.
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.
Back to Setting Up Python for AI.
Related guides
- Setting Up Python for AI — the main guide for this section, with the full setup track.
- How to Install Python for AI on Windows — the same workflow for a Windows machine.
- Create a Python Virtual Environment for AI — go deeper on virtual environments and dependency management.
- Python AI Fundamentals for Non-Developers — the foundational concepts behind everything in this section.