Python is one of the most powerful programming languages, mostly used in data science, machine learning, and big data analytics.
Contents
Method 1: Install Python via Official Installer
Step 1: Download Python
- Visit python.org/downloads.
- Select the latest Python 3.x release (e.g., Python 3.12).
Step 2: Run the Installer
- Open the
.pkg
file and follow the prompts. - Check “Add Python to PATH” during installation.
Step 3: Verify Installation
python3 --version # Should return "Python 3.x.x"
pip3 --version # Check if pip is installed
Method 2: Install Python via Homebrew
Step 1: Install Homebrew (if missing)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install Python
brew install python
Step 3: Verify
which python3 # Should show path like /usr/local/bin/python3
Method 3: Install Multiple Versions with pyenv
Step 1: Install pyenv
brew install pyenv
Step 2: Add to Shell Config
Add these lines to ~/.zshrc
(or ~/.bashrc
):
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Step 3: Install Python Versions
pyenv install 3.12.0 # Install specific version
pyenv global 3.12.0 # Set default version
Method 4: Use Xcode Command Line Tools
Step 1: Install Xcode CLI Tools
xcode-select --install
Step 2: Verify Python
python3 --version # macOS includes Python 3.9+ by default (may be outdated)
Check Preinstalled Python
Most Macs come with Python 2.7 (deprecated) and Python 3.x (minimal).
python --version # Python 2.7.x (do not use)
python3 --version # System Python 3.x (if present)
Troubleshooting Common Issues
1. python3
Command Not Found
- Ensure Python 3 was installed correctly.
- Update
PATH
in~/.zshrc
:
export PATH="/usr/local/bin:$PATH"
2. Conflicts Between Python Versions
Use pyenv
to manage versions or create virtual environments:
python3 -m venv myenv
source myenv/bin/activate
3. Homebrew Permissions Issues
Fix ownership:
sudo chown -R $(whoami) /usr/local/*
Which Method Should You Use?
Method | Best For | Pros | Cons |
---|---|---|---|
Official | Beginners, single Python version | Simple, GUI-based | Manual updates |
Homebrew | Developers needing latest versions | Easy updates, integrates with CLI | Requires Homebrew setup |
pyenv | Multiple Python versions/projects | Version flexibility | Slightly complex setup |
Xcode CLI | Minimal setup (uses system Python) | No additional installs | Outdated Python version |
Recommended Workflow
- Use Homebrew or pyenv for development.
- Always create virtual environments (
venv
orpipenv
) for projects. - Avoid modifying the system Python (
/usr/bin/python3
).
Next Steps
pip3 install pandas numpy