Python for AI

Gearing New Mac for Python and AI Development

In my last post, I walked through setting up my new Mac Mini M4 for a modern, efficient, and zenful development experience. Now, let’s take it a step further by equipping your Mac with the essential Python prerequisites for AI-related work. As always, we’ll stick to XDG standards where possible, ensuring a clean and organized environment.

Step 1: Install pyenv for Python Version Management

Python version management is crucial for AI development. We’ll use pyenv to manage Python installations.

brew install pyenv

Set up pyenv to be XDG-compliant.

mkdir -p $XDG_DATA_HOME/pyenv

Add the following lines to your ~/.zshenv and ~/.zshrc file:

~/.zshenv
export PYENV_ROOT="$XDG_DATA_HOME/pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
~/.zshrc
eval "$(pyenv init -)"

Step 2: Configure pip to Follow XDG Standards

To keep things organized, configure pip to use XDG directories.

mkdir -p $XDG_CACHE_HOME/pip
mkdir -p $XDG_CONFIG_HOME/pip

Add the following lines to ~/.zshenv:

~/.zshenv
export PIP_CACHE_DIR="$XDG_CACHE_HOME/pip"
export PIP_CONFIG_FILE="$XDG_CONFIG_HOME/pip/pip.conf"

Step 3: Install Python 3.11

Python 3.11 is currently optimal for AI work as of March 2025. Install and set it as the global version.

pyenv install 3.11.11
pyenv global 3.11.11

Verify the installation:

python --version

Update pip to the latest version:

python -m pip install --upgrade pip

Step 3: Install Pipx

Install pipx for installing Python tools in isolated environments:

python -m pip install --user pipx
mkdir -p $XDG_DATA_HOME/pipx

Add the following lines to ~/.zshenv:

~/.zshenv
export PIPX_HOME="$XDG_DATA_HOME/pipx"
export PIPX_BIN_DIR="$XDG_BIN_HOME"

Ensure directory where pipx stores apps:

python -m pipx ensurepath

Install virtualenv for environment management:

pip install virtualenv

Install pipx for isolated command-line applications:

pip install pipx
pipx ensurepath

Step 4: Create a Virtual Environment for AI Development

Keeping AI projects in an isolated virtual environment is a best practice.

mkdir -p $XDG_DATA_HOME/virtualenvs
python -m venv $XDG_DATA_HOME/virtualenvs/ai

To set up an alias for quick activation, add this line to ~/.zaliases:

~/.zaliases
alias ai-dev="source $XDG_DATA_HOME/virtualenvs/ai/bin/activate"

Now you can activate the environment by running:

ai

Update pip for the environment:

python -m pip install --upgrade pip

Deactivate the environment:

deactivate