Jason Abed | GitHub Pages

Simplifying Python Setup on macOS

Simplifying Python Setup on macOS: A Complete Guide to Clean Installation and Management

Managing Python on macOS can be tricky. Between the system-installed version, Homebrew, and Python.org downloads, conflicts often arise. Add in the need for multiple Python versions or isolated dependencies, and things can quickly get out of hand. In this guide, you’ll learn how to set up Python on macOS in a clean, organized way—perfect for development, safe for your system, and adaptable to different projects.


TL;DR

For a quick overview:

  • Install Python using Homebrew for a clean and manageable setup.
  • Use pyenv to manage multiple Python versions effortlessly.
  • Always use virtual environments to isolate dependencies and avoid conflicts.

Now, let’s dive into the details.


Why Python on macOS Can Be Confusing

macOS includes a system version of Python, primarily for internal tools. However:

  • System Python is outdated and not recommended for development.
  • Installing Python from multiple sources (e.g., Homebrew, Pyenv, Python.org) can lead to version conflicts.
  • Without proper dependency management, projects can overwrite or corrupt shared libraries.

The Ideal Python Setup on macOS

This guide will walk you through the steps to set up Python cleanly, manage multiple versions, and avoid dependency issues.


Step 1: Understand Your Current Setup

First, locate all Python versions on your system:

which -a python python3

Common locations include:

  • /usr/bin/python or /usr/bin/python3: System Python (do not modify).
  • /opt/homebrew/bin/python3: Homebrew-installed Python (preferred).
  • /Library/Frameworks/Python.framework: Python.org installer (optional to remove).

Step 2: Remove Redundant Python Installs

If you’ve installed Python via Python.org and want a cleaner setup:

  1. Remove Frameworks:

     sudo rm -rf /Library/Frameworks/Python.framework
     sudo rm -rf /Applications/Python*
    
  2. Remove Symlinks:

     sudo rm -f /usr/local/bin/python3*
    
  3. Clean Leftovers:

     rm -rf ~/Library/Python
    
  4. Verify Removal:

     which python3
    

This ensures you avoid conflicts between system, Homebrew, and Python.org installations.


Step 3: Install Python Using Homebrew

Homebrew provides a clean, managed Python installation:

  1. Install Homebrew:

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

     brew install python
    
  3. Verify Installation:

     python3 --version
     which python3
    

Homebrew installs Python in /opt/homebrew/bin/python3 (Apple Silicon) or /usr/local/bin/python3 (Intel).


Step 4: Use pyenv to Manage Multiple Python Versions

pyenv is a tool for managing multiple Python versions, essential for projects with version-specific requirements.

  1. Install pyenv:

     brew install pyenv
    
  2. Install Specific Versions:

     pyenv install 3.8.10
     pyenv install 3.11.5
    
  3. Set Global or Local Versions:

    • Global version:

        pyenv global 3.11.5
      
    • Directory-specific version:

        pyenv local 3.8.10
      
  4. Verify the Version:

     python3 --version
    

pyenv keeps Python versions in ~/.pyenv/versions, isolating them from system Python.


Step 5: Use Virtual Environments for Dependency Management

Virtual environments isolate dependencies for each project, preventing conflicts between libraries.

  1. Create a Virtual Environment:

     python3 -m venv my_project_env
    
  2. Activate the Environment:

     source my_project_env/bin/activate
    
  3. Install Project-Specific Dependencies:

     pip install flask
    
  4. Deactivate the Environment:

     deactivate
    

Step 6: Configure Your $PATH

Your $PATH determines which Python version is used by default. Set it up to prioritize Homebrew or pyenv Python over system Python.

  1. Edit .zshrc:

     nano ~/.zshrc
    
  2. Add This Line:

     export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
    
  3. Save and Reload:

     source ~/.zshrc
    
  4. Confirm:

     echo $PATH
    

Tips for a Clean and Productive Setup

1. Use a .gitignore for Virtual Environments

Add the following to your .gitignore to avoid committing virtual environments to your repository:

venv/

2. Automate with Aliases

Add aliases to your shell configuration for frequently used commands:

alias activate="source venv/bin/activate"

3. Use Tools for Productivity

  • pipx: Manage global Python CLI tools:

      pipx install black
    
  • poetry: Simplify dependency and environment management:

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

4. Troubleshooting Common Issues

  • Problem: python3: Command not found

    • Solution: Ensure Python is installed and $PATH is configured correctly.
  • Problem: Virtual environment fails to activate.

    • Solution: Use source venv/bin/activate and check shell compatibility.

Summary of Python Locations

Task Tool Location
System Python Pre-installed /usr/bin/python3
Managed Python Installation Homebrew /opt/homebrew/bin/python3
Multiple Python Versions Pyenv ~/.pyenv/versions/<version>
Isolated Dependencies Virtualenv path_to_env/bin/python

Conclusion

By following this guide, you’ll have a clean, efficient Python setup that’s easy to manage and adaptable to any project. Whether you’re building web apps, running data analysis, or experimenting with machine learning, this environment will keep your tools organized and your workflow smooth.

If you found this guide helpful, share it with others and feel free to leave feedback or questions!