No history yet

MacOS Environment Setup

Manage Packages with Homebrew

Your Mac already comes with a version of Python, but it's often outdated and deeply integrated into the operating system. Modifying this system Python can lead to unexpected problems. A better approach is to install and manage your own, separate version. The best tool for this on macOS is Homebrew.

Homebrew is a package manager for macOS. Think of it as an App Store for command-line tools. It simplifies the process of installing, updating, and managing software that doesn't come with a standard graphical interface.

Before installing, you might want to check if it's already on your system. You can do this by opening your Terminal app and running the command brew --version.

If it's installed, you'll see a version number. If not, you'll get a "command not found" error. To install Homebrew, paste the following command into your terminal and press Enter. The script will explain what it's going to do and prompt you for your password before it starts.

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

The installation might take a few minutes. Once it's done, the installer will give you one or two commands to run to add Homebrew to your system's PATH. This is a crucial step that tells your shell where to find the brew command. Go ahead and run those commands now.

Install and Configure Python

With Homebrew ready, installing a modern version of Python is straightforward. You'll use a single command to get the latest stable release.

brew install python3

Homebrew installs Python and its related tools, including pip, the Python package installer. However, your system needs to know to use this version of Python instead of the one that came with macOS. This is handled by configuring your shell's startup file.

On modern macOS, the default shell is Zsh, and its configuration file is .zshrc, located in your home directory (~/). You need to add a line to this file to ensure the Homebrew-installed binaries are found first.

# Open the .zshrc file in a simple text editor
nano ~/.zshrc

# Add this line at the end of the file
export PATH="/usr/local/bin:$PATH"

# Save the file (Ctrl+O, Enter) and exit (Ctrl+X)

After saving the file, you must either restart your terminal or run source ~/.zshrc for the changes to take effect. Now, when you type python3, your shell will find Homebrew's version in /usr/local/bin before it finds the system version.

You can verify this by checking the path and version:

which python3
# Expected output: /usr/local/bin/python3

python3 --version
# Expected output: Python 3.x.x (a recent version)
Lesson image

Isolate Project Dependencies

The first, and most crucial, step is creating an isolated workspace using a virtual environment.

When you work on multiple Python projects, they often have different dependencies. One web scraping project might need requests version 2.25, while another needs version 2.28. Installing these system-wide can create conflicts. The solution is to create isolated environments for each project using Python's built-in venv module.

venv

noun

A standard Python library for creating lightweight virtual environments with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary and can have its own independent set of installed Python packages in its site directories.

Let's create a directory for a new scraping project and set up a virtual environment inside it.

# 1. Create and navigate to your project folder
mkdir my-scraper
cd my-scraper

# 2. Create a virtual environment named 'env'
python3 -m venv env

This creates a new folder named env inside my-scraper. This folder contains a copy of the Python interpreter and a place to install project-specific libraries. To use it, you need to activate it.

source env/bin/activate

You'll know it's active because your terminal prompt will change to show (env) at the beginning. Now, any packages you install with pip will be placed inside the env folder, leaving your global Python installation untouched. To deactivate the environment, simply type deactivate.

A best practice for managing dependencies is to use a requirements.txt file. This file lists all the packages your project needs. You can create one and install from it using pip.

# Create a requirements file
echo "requests==2.28.1" > requirements.txt
echo "beautifulsoup4==4.11.1" >> requirements.txt

# Install all packages listed in the file
pip install -r requirements.txt

This command tells pip to read the specified file and install the exact versions of the packages listed. This makes your project reproducible for anyone who wants to run it. They just need to create their own virtual environment and run the same pip install -r requirements.txt command.

Check your understanding of the key concepts for setting up your macOS environment.

Quiz Questions 1/5

Why is it generally recommended to install a separate version of Python on macOS instead of using the pre-installed one?

Quiz Questions 2/5

After adding the Homebrew directory to your PATH in ~/.zshrc, what must you do for the changes to apply to your current terminal session?

Now that your environment is configured, you have a stable and isolated foundation for building web scraping tools. You can create a new virtual environment for every project, ensuring your dependencies are always managed cleanly.