80

While trying to install Python 3.6.6 (for Airflow) using PyEnv on MacOS, I am encountering build failure with following stack-trace

File
"/private/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347/Python-3.6.6/Lib/xmlrpc/client.py",
line 138, in <module>
    from xml.parsers import expat   File "/private/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347/Python-3.6.6/Lib/xml/parsers/expat.py",
line 4, in <module>
    from pyexpat import * ModuleNotFoundError: No module named 'pyexpat' make: *** [install] Error 1

BUILD FAILED (OS X 10.13.6 using python-build 20180424)

Inspect or clean up the working tree at
/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347
Results logged to /var/folders/6y/kf699bqj2sgcgjsh

I've followed the steps mentioned in the docs for setting-up PyEnv on Mac via Homebrew.


Environment / Framework versions

  • MacOS High Sierra 10.13.6
  • Homebrew 1.7.1
  • PyEnv 1.2.6
  • Existing Python distributions
    • Python 2.7.15 [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
    • Python 3.7.0 [Clang 9.0.0 (clang-900.0.39.2)] on darwin
y2k-shubham
  • 10,183
  • 11
  • 55
  • 131
  • Same issue, just had to open a new terminal window. Had some bad environment variables from when I ran `pyenv uninstall` in the same session. – william_grisaitis Jul 17 '22 at 19:04

23 Answers23

42

I had this problem with Mojave and Python 3.7.3.

This worked for me:

SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk MACOSX_DEPLOYMENT_TARGET=10.14 pyenv install 3.7.3
Henrique Bastos
  • 809
  • 8
  • 8
  • Works for Python 3.6.6 too. Thank you! – Matt Browne Jul 04 '19 at 11:42
  • 1
    Worked great on Mojave 10.14.5. The other answers did not work. – Kjell Hedström Jul 08 '19 at 16:32
  • 1
    Worked great on macOS Mojave 10.14.6 – Tachikoma Oct 22 '19 at 10:38
  • Unfortunately, not working for me on Mojave with Python 3.7.3 or 3.5.2 – WhooNo Mar 24 '20 at 20:32
  • 1
    Worked for me on macOS Catalina (10.15.3) with Python 3.7.3 – Srikanth May 07 '20 at 12:35
  • 22
    Anybody cares to explain what this does to newbies :)? – Bendemann Jan 26 '21 at 17:27
  • 1
    Is there some way to setup the environment so we dont have to include `SDKROOT` and `MACOSX_DEPLOYMENT_TARGET` every time? – Marcel Wilson Mar 08 '21 at 21:40
  • I get this: `By default, distutils will build C++ extension modules with "clang++".` With the error `checking for clang++... no`. But clang/clang++ are 100% already installed globally. Running the code in this answer above prompts me to install clang, which seemed promising. Maybe my pyenv couldn't find clang++? But DOH! Still failed `pyenv install` – Mote Zart May 18 '22 at 17:32
  • @MarcelWilson I don't know how to completely avoid the need for the variables, but you can add `export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk MACOSX_DEPLOYMENT_TARGET=12.3` your `.bashrc` or `.zshrc` so you don't have explicitly prefix every invocation of `pyenv`. This is only a workaround, mind you. It might fail when you upgrade the Xcode command-line tools. – Marcelo Cantos Aug 13 '22 at 04:32
  • Incidentally, my version number is different, since I'm on macOS Monterey. I added the following to my `.zshrc`, which attempts to detect the latest version number. It might be more robust or less than just hard-coding it (no warranty express or implied): `export SDKROOT=$(/bin/ls -d /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX*.*.sdk | /usr/bin/tail -1)` and `export MACOSX_DEPLOYMENT_TARGET=$(/bin/echo $SDKROOT | /usr/bin/sed -E 's/^.*[^[:digit:]]([[:digit:]]+\.[[:digit:]]+)\.sdk$/\1/')` – Marcelo Cantos Aug 13 '22 at 04:53
  • I found I was able to get away with adding just this to my .bash_profile : `export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk` – Marcel Wilson Aug 15 '22 at 15:22
31

I'm posting my solution for those who are trying to resolve this issue in 2021. I am using macOS Big Sur Version 11.5.1. For me, this worked:

  1. Uninstall the old tools

    $ sudo rm -rf /Library/Developer/CommandLineTools

  2. Reinstall Xcode Command line tools

    $ xcode-select --install

  3. Install python using pyenv

    $ pyenv install 3.9.5

enter image description here

Hailey Yoon
  • 477
  • 4
  • 5
27

The only way I could get it working is through the below prefixes before installing

CFLAGS="-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include 
-I$(xcrun --show-sdk-path)/usr/include" \
LDFLAGS="-L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib" \
PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 \
pyenv install -v 3.7.1
Srinivas Gowda
  • 371
  • 5
  • 17
  • 4
    If you're copy-pasting the snippet above, make sure to remove the new line at the end of the first line which can break the compilation – Nasir Aug 12 '21 at 12:49
14

I was able to get away with above error by referring to Common build problems

  • brew install readline xz
  • xcode-select --install
  • brew install openssl

After this, I installed and initialized pyenv

  • brew install pyenv
  • pyenv init (inside your project directory)

Finally install and activate required python version

  • pyenv install 3.6.6
  • pyenv local 3.6.6 (inside your project directory)

Here's the link to original thread #1188 on GitHub by @Harry Moreno

y2k-shubham
  • 10,183
  • 11
  • 55
  • 131
12

The accepted answer didn't work for me (Mojave) but this did:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Credit to http://www.blog.howechen.com/macos-mojave-pyenv-install-multi-version-build-failed-solution/

Callum M
  • 1,635
  • 19
  • 27
  • I suppose that might work, but is that a good idea? Doesn't this prevent you from using different SDK versions at different times? Presumably Apple changed this for a reason... – wjwwood Feb 16 '19 at 22:25
  • Works. Hopefully it doesn't break other things. – osprey May 16 '19 at 16:52
  • This is a known requirement for Mojave as described here https://github.com/pyenv/pyenv/wiki/Common-build-problems which I routinely completed during initial installation but still got the same error – NeilG Jun 26 '19 at 04:02
8

install Xcode:

xcode-select --install

install Brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

OR

update brew:

brew upgrade

install packages:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force
brew install openssl

Install Pyenv

curl https://pyenv.run | bash

Insert the following lines to .bashrc/.zshrc config file:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

restart the terminal and execute:

pyenv install 3.6.5

If still doesn't work: (just change the python version to the desired one - in my case i've installed python 3.6.5 to pyenv)

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.6.5 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
mark kats
  • 119
  • 1
  • 5
3

short answer:

$ brew upgrade

now you can try to install python through pyenv

$ pyenv install 3.7.4

then, you have to set the python path

pyenv global 3.7.4

now, close and open a new terminal and write the command

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

That's all. For me it was fine.

  • 1
    I tried this and it didn't work. The brew upgrade part worked fine (took a while) but then pyenv install 3.7.4 gave BUILD FAILED (OS X 11.1 using python-build 20180424) Inspect or clean up the working tree at /var/folders/m8... – Tony M Jan 11 '21 at 13:59
2

In my case this was caused by migration of data from old to new Mac. Both Xcode Command Line Tools and Homebrew migrated, but something broke. Reinstalling them from scratch worked for me.

Reinstall CLI tools:

$ sudo rm -rf /Library/Developer/CommandLineTools

$ xcode-select --install

Reinstall and update Homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew update

Istall pyenv + packages and python 3:

brew install pyenv
brew install openssl readline sqlite3 xz zlib
pyenv install 3.7.5
Vadim Tikanov
  • 631
  • 1
  • 9
  • 26
2

The cause is a change in Apple Clang 13.0.0+ that made it incompatible with existing Python versions. Upgrade to the aforementioned newest Python versions to fix the issue.

Please try the following suggestion https://github.com/pyenv/pyenv/issues/2112#issuecomment-1096478446

Peter Bean
  • 21
  • 2
1

same issue solved following a comment on pyenv issue tracking system suggesting to apply this patch: https://github.com/python/cpython/commit/8ea6353.patch

# original comment
pyenv install --patch 3.8.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

it did work for 3.6.9 and 3.7.4 versions as well e.g.

pyenv install --patch 3.6.9 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

source: https://github.com/pyenv/pyenv/issues/1738#issuecomment-880517959

Matteo Mazza
  • 75
  • 1
  • 8
1

I tried xcode install, brew upgrade etc, nothing worked. In the end , this patch worked (for MacOS Monterey)

pyenv install --patch 3.8.5 <<(curl -sSL https://raw.githubusercontent.com/Homebrew/formula-patches/113aa84/python/3.8.3.patch\?full_index\=1)
Fernando Freitas Alves
  • 3,709
  • 3
  • 26
  • 45
  • Welcome to stackoverflow! In oder to make your answer useful for others please format code as code with backtick-markups. Btw the link in your script seems to be broken. – Markus May 20 '22 at 14:24
1

I just had outdated prerequisites on MacOS 12.6

brew install openssl readline sqlite3 xz zlib tcl-tk

https://github.com/pyenv/pyenv/wiki#suggested-build-environment

jlooooo
  • 379
  • 3
  • 5
1

UPDATE FOR 2023

Python 3.6.X don't seem to support Apple Silicon. According to release note, python support Apple Silicon from 3.9.1.

SOLVED with the help of arch command:

arch -x86_64 pyenv install 3.8.12

Related to this issue: https://github.com/pyenv/pyenv/issues/1876

Carlos J. Ramirez
  • 1,049
  • 7
  • 9
0

After upgrading to Big Sur you need to reinstall X Code tools. Even if you had done it previously

xcode-select --install

This worked for me

If that doesn't work, force it to reset

sudo xcode-select --reset

More details can be found here: https://dev.to/o9uzdev/macos-xcrun-error-invalid-active-developer-path-missing-xcrun-411a

Musili Alfred M.
  • 3,085
  • 1
  • 15
  • 12
0

Try any of these:

❯ xcode-select --install
❯ xcode-select --reset
❯ export SDKROOT="macosx"

Maybe you don't have command line tools installed.
Or maybe you messed up the $DEVELOPER_DIR environment
variable path.
Or maybe your compiling with command line tools meant
for iPhone development or WatchOS !?!
❯ brew update && brew upgrade pyenv

Maybe your command versions aren't up to date ?
Double check you've properly followed the installation
steps for your version, OS and shell.
❯ brew install make cmake;
❯ export PATH="/usr/local/opt/make/libexec/gnubin:$PATH"

Maybe you don't have `make` and `cmake` build/compilation commands.
Or maybe they can't be found.
❯ brew install openssl readline tcl-tk sqlite3 xz zlib bzip2;
❯ export PATH="/usr/local/opt/bzip2/bin:$PATH";
export CPPFLAGS="\
    -I$(xcrun --show-sdk-path)/usr/include \
    -I$(brew --prefix openssl)/include \
    -I$(brew --prefix readline)/include \
    -I$(brew --prefix zlib)/include \
    -I$(brew --prefix bzip2)/include";
export LDFLAGS="\
    -L$(xcrun --show-sdk-path)/usr/lib \
    -L$(brew --prefix openssl)/lib \
    -L$(brew --prefix readline)/lib \
    -L$(brew --prefix zlib)/lib \
    -L$(brew --prefix bzip2)/lib";

Maybe you're missing some suggested `pyenv` build,
tools or what not. Or maybe they can't be found during
compilation.
https://github.com/pyenv/pyenv/wiki#suggested-build-environment
❯ unset LIBRARY

Maybe you've set/defined a LIBRARY environment variable
(either in your shell initialization/startup files,
e.g. .bashrc, .zshrc, etc; or shell session).
This can be bad because it could be used in the build/compilation
process and may crash it.
❯ pyenv install --verbose --patch 3.8.11 \
    < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Maybe you haven't heard of the magical patch yet ?
which fixes issue with build process not taking into
account certain MacOS versions.
Also using the `--verbose` option, might give you
better hints as to what's going wrong.

❯ pyenv global 3.8.11 && eval "$(pyenv init -)"
OR  
❯ pyenv global 3.8.11 && exec $SHELL --login

Also, don't forget to set your preferred global
python interpreter version and reinitialize `pyenv`
or restart your [login] shell altogether.

It may still not work, in that case just keep looking and post your answer when you've found it. Best Wishes.

Here are some of the resources I've consulted:

8c6b5df0d16ade6c
  • 1,910
  • 15
  • 15
0
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix 
bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk- 
path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew -- 
prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix 
bzip2)/lib" pyenv install --patch 3.6.12 < <(curl -sSL 
https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

change the version you want

Anas Ali
  • 67
  • 1
  • 3
0

for the recent googler, if you are using Big Sur, you should run the command

xcode-select --install
Yusuf
  • 2,295
  • 7
  • 15
  • 34
  • This has already been mentioned in many other answers. *When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.* – Eric Aya Nov 19 '21 at 13:37
0

I had the same error and this command worked for me, note that you need to change python version to your desired one to install, after --patch

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" \
pyenv install --patch 3.8.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
Yusuf
  • 2,295
  • 7
  • 15
  • 34
0

On Ventura 13.3.1 with XCode 14.1 I was able to get it resolved by:

  1. Open XCode -> Settings -> Locations
  2. Select the latest Command Line Tools
  3. Open new terminal and pyenv install 3.10.6
chardy
  • 1,233
  • 1
  • 10
  • 18
0

Following these 3 steps also solved my problem of installing pyenv 3.10.6 for macOS Monterey 12.6.5

  1. Uninstall the old tools

    $ sudo rm -rf /Library/Developer/CommandLineTools

  2. Reinstall Xcode Command line tools

    $ xcode-select --install

  3. Install python using pyenv

$ pyenv install 3.10.6
0

I had to uninstall and reinstall Home Brew before it returned to work. It concerned the change from Mac Intel to Mac M1(Silicon). See the article below from Josh Alletto to find out why.

HomeBrew on Mac M1/M2

-1

This steps helped me solving BUILD FAILED (OS X 13.0.1 using python-build 20180424)

  • First step run xcode-select --install
  • Second step brew install openssl@3
  • Finally brew install llvm and follow instructions for setting appropriate environment variables (in the output of the brew install command).
Vaghinak
  • 535
  • 6
  • 12
-3

This worked for me:

  1. remove CommandLineTools folder under /Library/Developer
  2. run in terminal: xcode-select --install
  3. sudo mv /usr/local/include /usr/local/include_old
Tomer
  • 1,058
  • 11
  • 18
  • 7
    Might work, but that's super destructive, and probably cause you even more problems in the end... – brandonscript Dec 03 '18 at 16:53
  • This is the only option that worked for me, but be cautious - after you perform this step, you need to recreate `include` dir and then reinstall everything from start with brew. Thanks @Tomer, this saved me from complete OS reinstall! – Vladimir Jovanović Jun 18 '19 at 05:03
  • further, I don't have a Packages folder under CommandLineTools – Brian Garr Apr 06 '21 at 14:42