75

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
1
  • 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. Jul 17, 2022 at 19:04

20 Answers 20

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
11
  • 1
    Worked great on Mojave 10.14.5. The other answers did not work. Jul 8, 2019 at 16:32
  • 1
    Worked great on macOS Mojave 10.14.6
    – Tachikoma
    Oct 22, 2019 at 10:38
  • 1
    Worked for me on macOS Catalina (10.15.3) with Python 3.7.3
    – Srikanth
    May 7, 2020 at 12:35
  • 19
    Anybody cares to explain what this does to newbies :)?
    – Bendemann
    Jan 26, 2021 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? Mar 8, 2021 at 21:40
26

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
1
  • 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, 2021 at 12:49
24

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

6
  • 2
    I can confirm this worked for me in MacOS Big Sur Ver 11.5.2. I uninstalled pyenv prior to performing the above steps to be safe then reinstalled after step 2 above with: curl https://pyenv.run | bash Sep 18, 2021 at 0:13
  • 1
    This worked for me, thanks.
    – Julian H
    Nov 10, 2021 at 16:40
  • 1
    This works on Big Sur 11.6.4 as well
    – StatsNoob
    Mar 6, 2022 at 17:41
  • 1
    This worked for me, Monterey 12.3.1 with pyenv 3.9.6. I did not uninstall pyenv first. Apr 21, 2022 at 14:11
  • 1
    Worked for me on Monterey 12.5.1, for python 3.10.6
    – bool.dev
    Aug 31, 2022 at 8:18
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

4
  • 4
    in my case it doesn't work, the error remains the same
    – Vadim
    Dec 7, 2018 at 11:11
  • This works if you have the LATEST version of XCode AND if you have "agreed" to the new license!
    – R Claven
    Jan 4, 2019 at 6:08
  • Also in my case did not work, but Srinivas Gowda solution did. Anyway, the link to the thread is useful.
    – psuzzi
    Mar 19, 2019 at 6:51
  • This worked to help me get the AWS ebs cli installed if you get this on Mac OS catalina Apr 7, 2020 at 5:17
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/

3
  • 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, 2019 at 22:25
  • Works. Hopefully it doesn't break other things.
    – osprey
    May 16, 2019 at 16:52
  • This is a known requirement for Mojave as described here github.com/pyenv/pyenv/wiki/Common-build-problems which I routinely completed during initial installation but still got the same error
    – NeilG
    Jun 26, 2019 at 4: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)
1
  • 1
    The --patch is the only thing that worked for me. Thanks.
    – Moshe
    Jun 17, 2021 at 9:17
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
  • 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, 2021 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
1
  • I was in the same situation of migrating and this was the only working solution. Jul 7, 2022 at 12:39
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)
1
  • 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, 2022 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

1

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

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

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:

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

0

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

0

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

xcode-select --install
1
  • 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, 2021 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)
0

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).
-2
BUILD FAILED (OS X 10.14.3 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/kt/79hj2dxs5vg7gsy188n6tk9c0000gn/T/python-build.20190226112205.71745
Results logged to /var/folders/kt/79hj2dxs5vg7gsy188n6tk9c0000gn/T/python-build.20190226112205.71745.log

Last 10 log lines:
  File "/private/var/folders/kt/79hj2dxs5vg7gsy188n6tk9c0000gn/T/python-build.20190226112205.71745/Python-3.6.6/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/private/var/folders/kt/79hj2dxs5vg7gsy188n6tk9c0000gn/T/python-build.20190226112205.71745/Python-3.6.6/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/kt/79hj2dxs5vg7gsy188n6tk9c0000gn/T/python-build.20190226112205.71745/Python-3.6.6/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/kt/79hj2dxs5vg7gsy188n6tk9c0000gn/T/python-build.20190226112205.71745/Python-3.6.6/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal

I found the solution from http://digidememory.blogspot.com/2019/01/macos-mojave-10142python371.html

In my case, I solved by this command

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
1
  • 2
    I tried every one of these on my M1 chip Mini, and none of them worked.
    – Brian Garr
    Apr 6, 2021 at 14:40
-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
3
  • 7
    Might work, but that's super destructive, and probably cause you even more problems in the end... Dec 3, 2018 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! Jun 18, 2019 at 5:03
  • further, I don't have a Packages folder under CommandLineTools
    – Brian Garr
    Apr 6, 2021 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.