“brew install python3” fails to compile pyexpat

high sierrahomebrewpythonxcode

brew install python3 fails with a pyexpat error, which means the installed Python can't parse XML (breaking pip etc). Here's the relevant excerpt from ~/Library/Logs/Homebrew/python/02.make:

*** WARNING: renaming "pyexpat" since importing it failed: dlopen(build/lib.macosx-10.13-x86_64-3.7/pyexpat.cpython-37m-darwin.so, 2): Symbol not found: _XML_ErrorString
  Referenced from: build/lib.macosx-10.13-x86_64-3.7/pyexpat.cpython-37m-darwin.so
  Expected in: flat namespace
 in build/lib.macosx-10.13-x86_64-3.7/pyexpat.cpython-37m-darwin.so

nm -gU /usr/lib/libexpat.1.dylib shows that this symbol is defined there, but according to otool -L the pyexpat .so file doesn't mention it; its only dependency is:

/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)

Again from 02.make, this is how the pyexpat .so was built (I've added line breaks to make it more readable):

clang \
-bundle \
-undefined dynamic_lookup \
-isysroot \
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk \
build/temp.macosx-10.13-x86_64-3.7/private/tmp/python-20180929-40812-11ki5rr/Python-3.7.0/Modules/pyexpat.o \
build/temp.macosx-10.13-x86_64-3.7/private/tmp/python-20180929-40812-11ki5rr/Python-3.7.0/Modules/expat/xmlparse.o \
build/temp.macosx-10.13-x86_64-3.7/private/tmp/python-20180929-40812-11ki5rr/Python-3.7.0/Modules/expat/xmlrole.o \
build/temp.macosx-10.13-x86_64-3.7/private/tmp/python-20180929-40812-11ki5rr/Python-3.7.0/Modules/expat/xmltok.o \
-L/usr/local/lib \
-o build/lib.macosx-10.13-x86_64-3.7/pyexpat.cpython-37m-darwin.so

I'm running XCode 9.4.1 on mac OS 10.13.6 (17G65). Other reports indicate that the problem might be that my Xcode version is newer than my macOS version, but due to work policy I can't upgrade to Mojave just yet.

Any ideas how I can get a working Python 3 install?

Best Answer

I eventually fixed this by

  • uninstalling XCode altogether (and relying on the xcode-select command line tools)
  • cherry-picking this commit into my homebrew-core Taps directory
  • reinstalling python3