J. Rogers, SE Ohio
How to recover a broken macOS Python environment by nuking Homebrew and starting fresh.
If your Homebrew installation has become unstable, is throwing "unsupported OS" errors, or has completely mangled your Python path, the best solution is often a "nuclear" reset: Switching to MacPorts.
MacPorts is stricter and more "sandboxed" than Homebrew, which often leads to a more stable system, but switching over comes with a few specific quirks you need to know to get running. And they have a specific version of MacPorts that is designed to run specifically on your old hardware and OS.
Phase 1: The Clean Sweep
Before installing the new system, you must completely remove the old one to prevent path conflicts.
1. Uninstall Homebrew
Run the official uninstall script. This removes the Homebrew binary and logic.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
2. Clean up Leftovers
The uninstaller might leave files behind. If you are on Apple Silicon (M1/M2/M3), remove the directory:
sudo rm -rf /opt/homebrew
(If on Intel Mac, check
Phase 2: Installing MacPorts & Python
MacPorts handles permissions differently than Homebrew (it requires sudo for installations), which keeps your system cleaner but requires specific setup steps.
Install Xcode Tools:
Install MacPorts:
Download the installer for your macOS version from The MacPorts Website and run the .pkg.
Update the Ports Tree:
Installing Python 3.12 correctly
MacPorts packages have specific names (usually py312-packagename).
Install Python and Pip:
sudo port install python312 py312-pip
Select the Default Version:
Unlike Homebrew, MacPorts doesn't automatically alias python to the version you just installed. You must do it manually:
sudo port select
sudo port select
sudo port select
Phase 3: Troubleshooting Common "Gotchas"
When moving to this environment, you will likely hit these four specific errors. Here is how to fix them.
Error 1: "error getting working directory name: not owner"
The Scenario: You try to run sudo port selfupdate.
The Error:
error getting working directory name: not owner
while executing "pwd"
The Cause: Your terminal is currently sitting in a protected directory (like ~/Downloads) that the sudo user is not allowed to verify due to macOS sandboxing.
The Fix: Simply move to your home directory before running the command.
cd ~
sudo port -v selfupdate
Error 2: "ls: .: Operation not permitted"
The Scenario: You type ls to list files, or your code fails to find local files.
The Error: Operation not permitted
The Cause: macOS Gatekeeper (TCC) has locked your Terminal app out of your files. This often happens after system updates or changing package managers.
The Fix: Grant Full Disk Access.
Go to System Settings > Privacy & Security > Full Disk Access.
Find Terminal in the list.
Toggle it OFF and then back ON.
Restart Terminal completely.
Error 3: "AttributeError: 'str' object has no attribute 'decode'"
The Scenario: You are running pip install -r requirements.txt.
The Error:
Getting requirements to build wheel ... error
AttributeError: 'str' object has no attribute 'decode'
Failed to build 'html'
The Cause: Your requirements.txt contains a package named html. This is a "zombie" package. It is ancient (from 2010), written for Python 2, and breaks in Python 3. You almost certainly intended to use Python's built-in html library, not this external package.
The Fix:
Open requirements.txt.
Delete the line that says html or html==1.16.
Run pip install -r requirements.txt again.
Error 4: "ModuleNotFoundError: No module named '_tkinter'"
The Scenario: You try to run a script that uses a GUI (like customtkinter).
The Error: ModuleNotFoundError: No module named '_tkinter'
The Cause: MacPorts is modular. To save space, the base Python installation does not include the GUI windowing tools (tkinter) by default.
The Fix: Install the tkinter binding specifically for your Python version.
sudo port install py312-tkinter
Summary of Commands
If you need to set this up again in the future, here is the cheat sheet:
cd ~
sudo port -v selfupdate
sudo port install python312 py312-pip py312-tkinter
sudo port select --set python python312
sudo port select --set pip pip312
No comments:
Post a Comment