ImportError: No module named zlib on macOS Catalina

November 22, 2019

A few times now, I've had to get around an issue that happens on macOS Catalina where you try to run a Python script and it fails with an import error:

ImportError: No module named zlib

The stacktrace will differ based on what was trying to import zlib, but the fix for me (on the most recent Catalina builds) was to reinstall python2 or python3 via brew. As some others have noted, it's important to xcode-select --install first, if you haven't.

If the script is Python 2:

brew reinstall python2

If the script is Python 3:

brew reinstall python3